Skip to content

Commit

Permalink
feat(core): add customer information onto the session (#1156)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanceaclark committed Jul 25, 2024
1 parent 616511c commit 7d91478
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-shirts-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": minor
---

Adds customer information onto the session for consumption in both server and client components
46 changes: 39 additions & 7 deletions core/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cookies } from 'next/headers';
import NextAuth, { type NextAuthConfig } from 'next-auth';
import NextAuth, { type DefaultSession, type NextAuthConfig } from 'next-auth';
import 'next-auth/jwt';
import CredentialsProvider from 'next-auth/providers/credentials';
import { z } from 'zod';

Expand All @@ -20,9 +21,18 @@ const config = {
signIn: '/login',
},
callbacks: {
jwt: ({ token, user }) => {
// user can actually be undefined
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (user?.id) {
token.id = user.id;
}

return token;
},
session({ session, token }) {
if (token.sub) {
session.user.id = token.sub;
if (token.id) {
session.user.id = token.id;
}

return session;
Expand Down Expand Up @@ -63,14 +73,16 @@ const config = {
async authorize(credentials) {
const { email, password } = Credentials.parse(credentials);

const customer = await login(email, password);
const response = await login(email, password);

if (!customer) {
if (!response.customer) {
return null;
}

return {
id: customer.entityId.toString(),
id: response.customer.entityId.toString(),
name: `${response.customer.firstName} ${response.customer.lastName}`,
email: response.customer.email,
};
},
}),
Expand All @@ -83,10 +95,30 @@ const getSessionCustomerId = async () => {
try {
const session = await auth();

return session?.user?.id;
return session?.user.id;
} catch {
// No empty
}
};

export { handlers, auth, signIn, signOut, getSessionCustomerId };

declare module 'next-auth' {
interface Session {
user: {
id: string;
} & DefaultSession['user'];
}

interface User {
id?: string;
name?: string | null;
email?: string | null;
}
}

declare module 'next-auth/jwt' {
interface JWT {
id?: string;
}
}
5 changes: 4 additions & 1 deletion core/client/mutations/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const LOGIN_MUTATION = graphql(`
login(email: $email, password: $password) {
customer {
entityId
firstName
lastName
email
}
}
}
Expand All @@ -17,5 +20,5 @@ export const login = async (email: string, password: string) => {
variables: { email, password },
});

return response.data.login.customer;
return response.data.login;
};

0 comments on commit 7d91478

Please sign in to comment.