You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

59 lines
1.5 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. accessToken?: string;
  6. refreshToken?: string;
  7. }
  8. export const authOptions: AuthOptions = {
  9. debug: process.env.NODE_ENV === "development",
  10. providers: [
  11. CredentialsProvider({
  12. id: "credentials",
  13. name: "Credentials",
  14. credentials: {
  15. username: { label: "Username", type: "text" },
  16. password: { label: "Password", type: "password" },
  17. },
  18. async authorize(credentials, req) {
  19. const res = await fetch(LOGIN_API_PATH, {
  20. method: "POST",
  21. body: JSON.stringify(credentials),
  22. headers: { "Content-Type": "application/json" },
  23. });
  24. const user = await res.json();
  25. if (res.ok && user) {
  26. return user;
  27. }
  28. return null;
  29. },
  30. }),
  31. ],
  32. pages: {
  33. signIn: "/login",
  34. },
  35. callbacks: {
  36. jwt(params) {
  37. // Add the data from user to the token
  38. const { token, user } = params;
  39. const newToken = { ...token, ...user };
  40. return newToken;
  41. },
  42. session({ session, token }) {
  43. const sessionWithToken: SessionWithTokens = {
  44. ...session,
  45. // Add the data from the token to the session
  46. accessToken: token.accessToken as string | undefined,
  47. refreshToken: token.refreshToken as string | undefined,
  48. };
  49. return sessionWithToken;
  50. },
  51. },
  52. };