Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 5f2971a

Browse files
committed
feat(web): add sign in example on page
1 parent b6e28dc commit 5f2971a

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

web/src/app/api/auth/[...nextauth]/route.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ import CredentialsProvider from "next-auth/providers/credentials";
22
import NextAuth from "next-auth/next";
33
import { signIn } from "@/lib/strapi/auth";
44

5+
type StrapiResponse = {
6+
jwt: string;
7+
user: {
8+
id: number;
9+
username: string;
10+
email: string;
11+
provider: string;
12+
confirmed: boolean;
13+
blocked: boolean;
14+
createdAt: string;
15+
updatedAt: string;
16+
};
17+
error?: {
18+
details: string[];
19+
};
20+
};
21+
522
const authOptions = {
623
secret: process.env.NEXTAUTH_SECRET,
724
providers: [
@@ -14,14 +31,19 @@ const authOptions = {
1431
async authorize(credentials) {
1532
try {
1633
if (credentials?.email == null || credentials.password == null) return null;
17-
const strapiResponse = await signIn(credentials.email, credentials.password);
18-
console.log("juhu", strapiResponse);
19-
if (strapiResponse.error) {
20-
console.error(strapiResponse.error.details);
2134

35+
const strapiResponse: StrapiResponse = await signIn(credentials.email, credentials.password);
36+
37+
if (strapiResponse.error) {
2238
return null;
2339
}
24-
return strapiResponse;
40+
41+
return {
42+
jwt: strapiResponse.jwt,
43+
id: String(strapiResponse.user.id),
44+
email: strapiResponse.user.email,
45+
name: strapiResponse.user.username,
46+
};
2547
} catch {
2648
return null;
2749
}
@@ -32,15 +54,15 @@ const authOptions = {
3254
session: async ({ session, token }: { session: any; token: any }) => {
3355
session.id = token.id;
3456
session.jwt = token.jwt;
35-
return Promise.resolve(session);
57+
return session;
3658
},
37-
jwt: async ({ token, user }) => {
38-
const isSignIn = user ? true : false;
39-
if (isSignIn) {
59+
jwt: async ({ token, user }: { token: any; user: any }) => {
60+
if (user) {
4061
token.id = user.id;
4162
token.jwt = user.jwt;
63+
token.email = user.email;
4264
}
43-
return Promise.resolve(token);
65+
return token;
4466
},
4567
},
4668
};

web/src/components/Layout/Navigation/Navigation.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Link from "next/link";
33
import Image from "next/image";
44
import { signIn, useSession, signOut } from "next-auth/react";
55
import { IconUserCircle } from "@tabler/icons-react";
6-
import { useEffect } from "react";
76

87
type NavigationProps = {
98
menu: Page[];
@@ -45,7 +44,7 @@ export default function Navigation({ menu }: NavigationProps) {
4544
</ul>
4645

4746
<div className="group mt-auto mb-8 hover:text-gray-2">
48-
{session && <button onClick={() => signOut()}>Sign out</button>}
47+
{session && <button onClick={() => signOut()}>Sign out {JSON.stringify(session.user)}</button>}
4948
<Link className="flex items-center gap-2 text-white group-hover:text-gray-2" href="/my-account" onClick={() => signIn()}>
5049
<IconUserCircle className="text-white group-hover:text-gray-2" size={40} />
5150
My Account

0 commit comments

Comments
 (0)