File tree 5 files changed +75
-12
lines changed
5 files changed +75
-12
lines changed Original file line number Diff line number Diff line change 1
1
import { useState , useRef } from "react" ;
2
2
import { signIn } from "next-auth/client" ;
3
+ import { useRouter } from "next/router" ;
3
4
4
5
import classes from "./auth-form.module.css" ;
5
6
@@ -17,6 +18,7 @@ async function createUser(email, password) {
17
18
}
18
19
19
20
function AuthForm ( ) {
21
+ const router = useRouter ( ) ;
20
22
const emailInputRef = useRef ( ) ;
21
23
const passwordInputRef = useRef ( ) ;
22
24
const [ isLogin , setIsLogin ] = useState ( true ) ;
@@ -36,7 +38,7 @@ function AuthForm() {
36
38
password : enteredPassword ,
37
39
} ) ;
38
40
if ( ! result . error ) {
39
- // set auth state
41
+ router . replace ( "/profile" ) ;
40
42
}
41
43
} else {
42
44
try {
Original file line number Diff line number Diff line change 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" ;
3
7
4
8
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
+ // }
6
26
7
27
return (
8
28
< section className = { classes . profile } >
Original file line number Diff line number Diff line change 1
1
import Head from "next/head" ;
2
+ import { Provider } from "next-auth/client" ;
2
3
3
4
import Layout from "../components/layout/layout" ;
4
5
import "../styles/globals.css" ;
5
6
6
7
function MyApp ( { Component, pageProps } ) {
7
8
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 >
14
17
) ;
15
18
}
16
19
Original file line number Diff line number Diff line change 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" ;
2
6
3
7
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
+
4
26
return < AuthForm /> ;
5
27
}
6
28
Original file line number Diff line number Diff line change 1
- import UserProfile from '../components/profile/user-profile' ;
1
+ import { getSession } from "next-auth/client" ;
2
+ import UserProfile from "../components/profile/user-profile" ;
2
3
3
4
function ProfilePage ( ) {
4
5
return < UserProfile /> ;
5
6
}
6
7
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
+
7
23
export default ProfilePage ;
You can’t perform that action at this time.
0 commit comments