Blame view

pages/api/auth/[...nextauth].js 5.11 KB
jay committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import axios from "axios";

/**
 * Reference:
 * https://next-auth.js.org/
 * https://strapi.io/blog/user-authentication-in-next-js-with-strapi
 * https://medium.com/@tom555my/strapi-next-js-email-password-authentication-a8207f72b446#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjQwMmYzMDViNzA1ODEzMjlmZjI4OWI1YjNhNjcyODM4MDZlY2E4OTMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJuYmYiOjE2NjE5NDM4MzEsImF1ZCI6IjIxNjI5NjAzNTgzNC1rMWs2cWUwNjBzMnRwMmEyamFtNGxqZGNtczAwc3R0Zy5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsInN1YiI6IjEwMzU4MDUxNzI0MDg5MTg5NDI5OSIsImVtYWlsIjoiaGFyaXNoLnJrLnBhdGVsQGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJhenAiOiIyMTYyOTYwMzU4MzQtazFrNnFlMDYwczJ0cDJhMmphbTRsamRjbXMwMHN0dGcuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJuYW1lIjoiSGFyaXNoIFBhdGVsIiwicGljdHVyZSI6Imh0dHBzOi8vbGgzLmdvb2dsZXVzZXJjb250ZW50LmNvbS9hLS9BRmRadWNxVlFSbE42TUQyeFJDVjhwMThlcXRSSjB0UmZpc2F1T21FeGVodFNnPXM5Ni1jIiwiZ2l2ZW5fbmFtZSI6IkhhcmlzaCIsImZhbWlseV9uYW1lIjoiUGF0ZWwiLCJpYXQiOjE2NjE5NDQxMzEsImV4cCI6MTY2MTk0NzczMSwianRpIjoiZDk1YzIyYzVkMjY1N2NmZWEwN2Q1M2QwMmZkMjY1NGVhMGE4NTA0OSJ9.F4xZZT2ebj3LvtOqFwC-EeneXUVzAEolss20Zz1JYpnWlfDmTAzJkStaXN_3n59X2jhppU2vLX426fihE1sjvfkbSFY3L--uMlGRjKbkKMzlhqppy2TUN1Qcsaqf8PSamLQxZx9Ta3qdmV4FCvf8FLXbiVmgX2KOYg3stitiL3vQpEDR2faAv-dlnaOSDbKU76elKf5VMVHGw9ed7dLeNF0qPRLXg_NrQ3dPBH1sDwfLh0g4elCMkneEtMjZY9ptLJxhc_bwuQpXs3xNwjlIAZA1gM60lhIiZESwlAnnpEBdIjPVsjoUeJEEM1bB8dP4hqndJPVwA80ZnqOOXiH8bQ
 */
export default NextAuth({
  // Configure one or more authentication providers
  providers: [
    CredentialsProvider({
      name: "Sign in with Email",
      credentials: {
        email: { label: "Email", type: "text" },
        password: { label: "Password", type: "password" }
      },
      async authorize(credentials, req) {
        /**
         * This function is used to define if the user is authenticated or not.
         * If authenticated, the function should return an object contains the user data.
         * If not, the function should return `null`.
         */
        if (credentials == null) return null;

        const { email, password } = credentials;

        // Check if email and password is specified.
        if (!email || !password) {
          throw new Error("Please enter email and password to login.");
        }

        /**
         * credentials is defined in the config above.
         * We can expect it contains two properties: `email` and `password`
         */
        try {
jay committed
40
          const userResponse = await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/auth/local`, {
jay committed
41 42 43 44 45
            identifier: email,
            password: password
          });

          // console.log("Axios login returned with data:");
jaymehta committed
46
          // console.log("userResponse", userResponse.data);
jay committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
          // console.log(jwt);

          // Response from the above call can be
          // 1. Incase of a successful authentication.
          // {
          //     "jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwiaWF0IjoxNjYxOTQzMzY1LCJleHAiOjE2NjQ1MzUzNjV9.kAN2ocQEqbjbR_-6-ogfZMplJIn1VHkCn3hoAW4QtmY",
          //     "user": {
          //         "id": 4,
          //         "username": "harish",
          //         "email": "harish@email.com",
          //         "provider": "local",
          //         "confirmed": true,
          //         "blocked": false,
          //         "createdAt": "2022-08-30T06:38:17.280Z",
          //         "updatedAt": "2022-08-31T10:03:59.430Z"
          //     }
          // }

          // 2. Incase of an invalid authentication.
          // {
          //     "data": null,
          //     "error": {
          //         "status": 400,
          //         "name": "ValidationError",
          //         "message": "Invalid identifier or password",
          //         "details": {}
          //     }
          // }

jaymehta committed
76 77 78 79 80 81 82
          return {
            ...userResponse.data.user,
            name: userResponse.data.user.email,
            jwt: userResponse.data.jwt,
            email: userResponse.data.user.email,
            user: userResponse.data.user
          };
jay committed
83 84 85 86 87 88 89 90 91 92 93
        } catch (error) {
          console.log("Error while fetching credentials:");
          console.log(error.response.data);

          // Sign In Fail
          // return null;
          throw new Error(error.response.data.error.message);
        }
      }
    })
  ],
jaymehta committed
94
  secret: process.env.NEXTAUTH_SECRET,
jay committed
95 96
  callbacks: {
    session: async ({ session, token }) => {
jaymehta committed
97 98
      // console.log("session 1", session);
      // console.log("session 2", token);
jay committed
99 100 101 102 103 104 105 106 107
      session.id = token.id;
      session.jwt = token.jwt;

      // console.log("session callback invoked: ");
      // console.log(session);

      return Promise.resolve(session);
    },
    jwt: async ({ token, user }) => {
jaymehta committed
108 109
      // console.log("user 1", user);
      // console.log("token 1", token);
jay committed
110

jay committed
111 112 113 114 115 116 117 118 119 120 121 122 123
      const isSignIn = user ? true : false;
      if (isSignIn) {
        token.id = user.id;
        token.jwt = user.jwt;
      }

      // console.log("jwt callback invoked: ");
      // console.log(token);

      return Promise.resolve(token);
    }
  }
});