Derock
Explore posts from serversTTCTheo's Typesafe Cult
•Created by Derock on 4/16/2023 in #questions
NextAuth "[next-auth][error][JWT_SESSION_ERROR]" (CredentialsProvider)
This is my first time using next-auth, and I am trying to get basic credentials-based authentication implemenmted.
/**
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
* object and keep type safety.
*
* @see https://next-auth.js.org/getting-started/typescript#module-augmentation
*/
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
profilePicture: string;
} & DefaultSession["user"];
}
interface User {
// ...other properties
// role: UserRole;
profilePicture: string;
}
}
/**
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
*
* @see https://next-auth.js.org/configuration/options
*/
export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
},
}),
},
secret: env.NEXTAUTH_SECRET!,
adapter: PrismaAdapter(prisma),
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
username: {
label: "Username",
type: "text",
},
password: {
label: "Password",
type: "password"
}
},
authorize: async (credentials, req) => {
return {
id: "1",
name: "John Smith",
email: "[email protected]",
profilePicture: "https://avatars.githubusercontent.com/u/1016365?v=4"
}
} // placeholder for now ^ - trying to rule out as many possible errors as i can
],
session: {
strategy: "jwt"
}
};
/**
* Wrapper for `getServerSession` so that you don't need to import the `authOptions` in every file.
*
* @see https://next-auth.js.org/configuration/nextjs
*/
export const getServerAuthSession = (ctx: {
req: GetServerSidePropsContext["req"];
res: GetServerSidePropsContext["res"];
}) => {
return getServerSession(ctx.req, ctx.res, authOptions);
};
/**
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
* object and keep type safety.
*
* @see https://next-auth.js.org/getting-started/typescript#module-augmentation
*/
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
profilePicture: string;
} & DefaultSession["user"];
}
interface User {
// ...other properties
// role: UserRole;
profilePicture: string;
}
}
/**
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
*
* @see https://next-auth.js.org/configuration/options
*/
export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
},
}),
},
secret: env.NEXTAUTH_SECRET!,
adapter: PrismaAdapter(prisma),
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
username: {
label: "Username",
type: "text",
},
password: {
label: "Password",
type: "password"
}
},
authorize: async (credentials, req) => {
return {
id: "1",
name: "John Smith",
email: "[email protected]",
profilePicture: "https://avatars.githubusercontent.com/u/1016365?v=4"
}
} // placeholder for now ^ - trying to rule out as many possible errors as i can
],
session: {
strategy: "jwt"
}
};
/**
* Wrapper for `getServerSession` so that you don't need to import the `authOptions` in every file.
*
* @see https://next-auth.js.org/configuration/nextjs
*/
export const getServerAuthSession = (ctx: {
req: GetServerSidePropsContext["req"];
res: GetServerSidePropsContext["res"];
}) => {
return getServerSession(ctx.req, ctx.res, authOptions);
};
10 replies