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

Commit b6e28dc

Browse files
committed
feat(web): add sign in function, to sign in to strapi
1 parent 527a536 commit b6e28dc

File tree

8 files changed

+170
-32
lines changed

8 files changed

+170
-32
lines changed

web/package-lock.json

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"dependencies": {
1515
"@tabler/icons-react": "^2.45.0",
1616
"@tanstack/react-table": "^8.11.7",
17+
"axios": "^1.6.8",
1718
"next": "13.5.6",
1819
"next-auth": "^4.24.7",
1920
"qs": "^6.11.2",

web/src/app/[...slug]/page.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ import { getPageContent } from "@/helpers/getPageContent";
33
import Card from "@/components/common/Card/Card";
44

55
export default async function page({ params }: { params: { slug: string[] } }) {
6-
const { page } = await getPageContent(params.slug);
7-
return (
8-
<div>
9-
{/* <h1>{page.pageTitle}</h1> */}
10-
<Card data={page}></Card>
11-
</div>
12-
);
6+
try {
7+
const { page } = await getPageContent(params.slug);
8+
return (
9+
<div>
10+
{/* <h1>{page.pageTitle}</h1> */}
11+
<Card data={page}></Card>
12+
</div>
13+
);
14+
} catch (e) {
15+
return (
16+
<div>
17+
<p>No content defined in CMS.</p>
18+
</div>
19+
);
20+
}
1321
}

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
1-
import GithubProvider from "next-auth/providers/github";
21
import CredentialsProvider from "next-auth/providers/credentials";
32
import NextAuth from "next-auth/next";
3+
import { signIn } from "@/lib/strapi/auth";
44

55
const authOptions = {
6+
secret: process.env.NEXTAUTH_SECRET,
67
providers: [
7-
GithubProvider({
8-
clientId: process.env.GITHUB_ID ?? "",
9-
clientSecret: process.env.GITHUB_SECRET ?? "",
10-
}),
118
CredentialsProvider({
12-
name: "Credentials",
9+
name: "Username or Email",
1310
credentials: {
14-
username: { label: "Username", type: "text", placeholder: "jsmith" },
11+
email: { label: "Email", type: "text" },
1512
password: { label: "Password", type: "password" },
1613
},
1714
async authorize(credentials) {
18-
const res = await fetch("/your/endpoint", {
19-
method: "POST",
20-
body: JSON.stringify(credentials),
21-
headers: { "Content-Type": "application/json" },
22-
});
23-
const user = await res.json();
15+
try {
16+
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);
2421

25-
if (res.ok && user) return user;
26-
else return null;
22+
return null;
23+
}
24+
return strapiResponse;
25+
} catch {
26+
return null;
27+
}
2728
},
2829
}),
2930
],
31+
callbacks: {
32+
session: async ({ session, token }: { session: any; token: any }) => {
33+
session.id = token.id;
34+
session.jwt = token.jwt;
35+
return Promise.resolve(session);
36+
},
37+
jwt: async ({ token, user }) => {
38+
const isSignIn = user ? true : false;
39+
if (isSignIn) {
40+
token.id = user.id;
41+
token.jwt = user.jwt;
42+
}
43+
return Promise.resolve(token);
44+
},
45+
},
3046
};
3147

3248
const handler = NextAuth(authOptions);

web/src/app/layout.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,10 @@ export default async function RootLayout({ children }: { children: React.ReactNo
4545
const session = await getServerSession();
4646

4747
return (
48-
<html lang="en">
49-
<body className={firaCode.className}>
50-
{pageProps && (
51-
<Layout pages={pageProps}>
52-
<SessionProvider session={session}>{children}</SessionProvider>
53-
</Layout>
54-
)}
55-
</body>
56-
</html>
48+
<SessionProvider session={session}>
49+
<html lang="en">
50+
<body className={firaCode.className}>{pageProps && <Layout pages={pageProps}>{children}</Layout>}</body>
51+
</html>
52+
</SessionProvider>
5753
);
5854
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"use client";
22
import Link from "next/link";
33
import Image from "next/image";
4+
import { signIn, useSession, signOut } from "next-auth/react";
45
import { IconUserCircle } from "@tabler/icons-react";
6+
import { useEffect } from "react";
57

68
type NavigationProps = {
79
menu: Page[];
@@ -17,6 +19,8 @@ export type Page = {
1719
};
1820

1921
export default function Navigation({ menu }: NavigationProps) {
22+
const { data: session } = useSession();
23+
2024
return (
2125
<header className="h-full rounded pt-8 bg-gray-5">
2226
<nav className="flex flex-col items-center h-full">
@@ -41,7 +45,8 @@ export default function Navigation({ menu }: NavigationProps) {
4145
</ul>
4246

4347
<div className="group mt-auto mb-8 hover:text-gray-2">
44-
<Link className="flex items-center gap-2 text-white group-hover:text-gray-2" href="/my-account">
48+
{session && <button onClick={() => signOut()}>Sign out</button>}
49+
<Link className="flex items-center gap-2 text-white group-hover:text-gray-2" href="/my-account" onClick={() => signIn()}>
4550
<IconUserCircle className="text-white group-hover:text-gray-2" size={40} />
4651
My Account
4752
</Link>

web/src/config/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const CMS_API_URL = process.env.CMS_API ?? "http://cms:1337/api";

web/src/lib/strapi/auth.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { CMS_API_URL } from "@/config/constants";
2+
3+
export const signIn = async (email: string, password: string) => {
4+
try {
5+
const response = await fetch(`${CMS_API_URL}/auth/local`, {
6+
method: "POST",
7+
headers: {
8+
"Content-Type": "application/json",
9+
},
10+
body: JSON.stringify({
11+
identifier: email,
12+
password: password,
13+
}),
14+
});
15+
16+
return await response.json();
17+
} catch {
18+
return null;
19+
}
20+
};

0 commit comments

Comments
 (0)