Skip to content

Instantly share code, notes, and snippets.

@vincentchalamon
Created August 3, 2021 18:26
Show Gist options
  • Save vincentchalamon/ef71a8903b5dc9b19ce95ff1a2840d31 to your computer and use it in GitHub Desktop.
Save vincentchalamon/ef71a8903b5dc9b19ce95ff1a2840d31 to your computer and use it in GitHub Desktop.
import NextAuth from "next-auth";
import Providers from "next-auth/providers";
import {fetch} from "utils/dataAccess";
const options = {
providers: [
Providers.Credentials({
name: 'Credentials',
authorize: async (credentials) => {
const response = await fetch("/login_check", {
method: "POST",
body: JSON.stringify({
username: credentials.username,
password: credentials.password,
}),
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
},
});
return response.data;
},
})
],
callbacks: {
async jwt(token, user) {
if (user) {
token.user = user;
}
// token is valid
if (new Date() < new Date(token.user.exp)) {
return token;
}
// token has expired: force user to login again
// todo implement refresh token
return null;
},
async session(session, token) {
if (token) {
session.token = token;
}
if (token.user) {
session.user = token.user;
}
return session;
},
},
pages: {
signIn: '/login',
error: '/login',
newUser: '/register',
},
}
export default (req, res) => NextAuth(req, res, options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment