Skip to content

Commit ab43cdb

Browse files
committed
add session handling
1 parent 4f8e8fc commit ab43cdb

File tree

5 files changed

+75
-12
lines changed

5 files changed

+75
-12
lines changed

components/auth/auth-form.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useState, useRef } from "react";
22
import { signIn } from "next-auth/client";
3+
import { useRouter } from "next/router";
34

45
import classes from "./auth-form.module.css";
56

@@ -17,6 +18,7 @@ async function createUser(email, password) {
1718
}
1819

1920
function AuthForm() {
21+
const router = useRouter();
2022
const emailInputRef = useRef();
2123
const passwordInputRef = useRef();
2224
const [isLogin, setIsLogin] = useState(true);
@@ -36,7 +38,7 @@ function AuthForm() {
3638
password: enteredPassword,
3739
});
3840
if (!result.error) {
39-
// set auth state
41+
router.replace("/profile");
4042
}
4143
} else {
4244
try {

components/profile/user-profile.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1-
import ProfileForm from './profile-form';
2-
import classes from './user-profile.module.css';
1+
// import { getSession } from "next-auth/client";
2+
// import { useState, useEffect } from "react";
3+
// import { useRouter } from "next/router";
4+
5+
import ProfileForm from "./profile-form";
6+
import classes from "./user-profile.module.css";
37

48
function UserProfile() {
5-
// Redirect away if NOT auth
9+
// alternative method if you don't want to use getServerSideProps on profile page
10+
// const router = useRouter();
11+
// const [isLoading, setIsLoading] = useState(true);
12+
13+
// useEffect(() => {
14+
// getSession().then((session) => {
15+
// if (!session) {
16+
// router.push("/auth");
17+
// } else {
18+
// setIsLoading(false);
19+
// }
20+
// });
21+
// }, []);
22+
23+
// if (isLoading) {
24+
// return <p className={classes.profile}>Loading...</p>;
25+
// }
626

727
return (
828
<section className={classes.profile}>

pages/_app.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import Head from "next/head";
2+
import { Provider } from "next-auth/client";
23

34
import Layout from "../components/layout/layout";
45
import "../styles/globals.css";
56

67
function MyApp({ Component, pageProps }) {
78
return (
8-
<Layout>
9-
<Head>
10-
<title>NextAuth</title>
11-
</Head>
12-
<Component {...pageProps} />
13-
</Layout>
9+
<Provider session={pageProps.session}>
10+
<Layout>
11+
<Head>
12+
<title>NextAuth</title>
13+
</Head>
14+
<Component {...pageProps} />
15+
</Layout>
16+
</Provider>
1417
);
1518
}
1619

pages/auth.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
import AuthForm from '../components/auth/auth-form';
1+
import { useRouter } from "next/router";
2+
import { getSession } from "next-auth/client";
3+
import { useEffect, useState } from "react";
4+
5+
import AuthForm from "../components/auth/auth-form";
26

37
function AuthPage() {
8+
// Alternative to getServerSideProps, see profile page
9+
const router = useRouter();
10+
const [isLoading, setIsLoading] = useState(true);
11+
12+
useEffect(() => {
13+
getSession().then((session) => {
14+
if (session) {
15+
router.replace("/");
16+
} else {
17+
setIsLoading(false);
18+
}
19+
});
20+
}, [router]);
21+
22+
if (isLoading) {
23+
return <p>Loading...</p>;
24+
}
25+
426
return <AuthForm />;
527
}
628

pages/profile.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
import UserProfile from '../components/profile/user-profile';
1+
import { getSession } from "next-auth/client";
2+
import UserProfile from "../components/profile/user-profile";
23

34
function ProfilePage() {
45
return <UserProfile />;
56
}
67

8+
export async function getServerSideProps(context) {
9+
const session = await getSession({ req: context.req });
10+
if (!session) {
11+
return {
12+
redirect: {
13+
destination: "/auth",
14+
permanent: false,
15+
},
16+
};
17+
}
18+
return {
19+
props: { session },
20+
};
21+
}
22+
723
export default ProfilePage;

0 commit comments

Comments
 (0)