log in as other user

This commit is contained in:
Dobromir Popov
2024-04-13 13:04:02 +03:00
parent 717cd8499d
commit 471cbc0a55
5 changed files with 65 additions and 16 deletions

View File

@ -7,7 +7,7 @@ import DayOfWeek from "../DayOfWeek";
import TextEditor from "../TextEditor";
import FileUploadWithPreview from 'components/FileUploadWithPreview ';
import ProtectedRoute, { serverSideAuth } from "../..//components/protectedRoute";
import ProtectedRoute, { serverSideAuth } from "../../components/protectedRoute";
import { UserRole } from "@prisma/client";
const common = require('src/helpers/common');

View File

@ -5,6 +5,7 @@ import { useEffect, useState } from 'react'
import toast from "react-hot-toast";
import axiosInstance from '../../src/axiosSecure';
import ProtectedRoute, { serverSideAuth } from "../../components/protectedRoute";
//add months to date. works with negative numbers and numbers > 12
export function addMonths(numOfMonths, date) {
@ -53,6 +54,23 @@ export default function PublisherCard({ publisher }) {
console.log(JSON.stringify(error));
}
};
const handleLoginAs = async (userId) => {
const response = await fetch('/api/auth/login-as', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ userId }),
});
if (response.ok) {
const data = await response.json();
// Assuming you have some context or state management to update the session
updateSession(data.session);
} else {
alert("Failed to impersonate user.");
}
};
return isCardVisible ? (
// className="block p-6 max-w-sm bg-white rounded-lg border border-gray-200 shadow-md hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700 mb-3"
@ -89,6 +107,10 @@ export default function PublisherCard({ publisher }) {
<path fillRule="evenodd" d="M4.293 4.293A1 1 0 015.707 3.707L10 8l4.293-4.293a1 1 0 111.414 1.414L11.414 9l4.293 4.293a1 1 0 01-1.414 1.414L10 10.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 9 4.293 4.707a1 1 0 010-1.414z" clipRule="evenodd" /> */}
</svg>
</button>
<ProtectedRoute>
<button onClick={() => handleLoginAs(publisher.id)}>Login as</button>
</ProtectedRoute>
</div>
<style jsx>{`
.cardFadeOut {