Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

67 rindas
1.8 KiB

  1. import { AuthOptions, Session } from "next-auth";
  2. import CredentialsProvider from "next-auth/providers/credentials";
  3. import { LOGIN_API_PATH } from "./api";
  4. export interface SessionWithTokens extends Session {
  5. abilities?: any[];
  6. accessToken?: string;
  7. refreshToken?: string;
  8. }
  9. export interface ability {
  10. actionSubjectCombo: string;
  11. }
  12. export const authOptions: AuthOptions = {
  13. debug: process.env.NODE_ENV === "development",
  14. providers: [
  15. CredentialsProvider({
  16. id: "credentials",
  17. name: "Credentials",
  18. credentials: {
  19. username: { label: "Username", type: "text" },
  20. password: { label: "Password", type: "password" },
  21. },
  22. async authorize(credentials, req) {
  23. const res = await fetch(LOGIN_API_PATH, {
  24. method: "POST",
  25. body: JSON.stringify(credentials),
  26. headers: { "Content-Type": "application/json" },
  27. });
  28. const user = await res.json();
  29. if (res.ok && user) {
  30. return user;
  31. }
  32. return null;
  33. },
  34. }),
  35. ],
  36. pages: {
  37. signIn: "/login",
  38. },
  39. callbacks: {
  40. jwt(params) {
  41. // Add the data from user to the token
  42. const { token, user } = params;
  43. const newToken = { ...token, ...user };
  44. return newToken;
  45. },
  46. session({ session, token }) {
  47. const sessionWithToken: SessionWithTokens = {
  48. ...session,
  49. // Add the data from the token to the session
  50. abilities: (token.abilities as ability[]).map(
  51. (item: ability) => item.actionSubjectCombo,
  52. ) as string[],
  53. accessToken: token.accessToken as string | undefined,
  54. refreshToken: token.refreshToken as string | undefined,
  55. };
  56. // console.log(sessionWithToken)
  57. return sessionWithToken;
  58. },
  59. },
  60. };