Merge pull request #430 from mihaic195/fix/next-auth-issues
Fix next-auth issues
This commit is contained in:
		
						commit
						77ada9667a
					
				
					 3 changed files with 37 additions and 7 deletions
				
			
		|  | @ -1,10 +1,10 @@ | ||||||
| import Head from "next/head"; | import Head from "next/head"; | ||||||
| import Link from "next/link"; | import Link from "next/link"; | ||||||
| import React from "react"; | import React from "react"; | ||||||
| import { getCsrfToken } from "next-auth/client"; | import { getCsrfToken, getSession } from "next-auth/client"; | ||||||
| import debounce from "lodash.debounce"; | import debounce from "lodash.debounce"; | ||||||
| 
 | 
 | ||||||
| export default function Page({ csrfToken }) { | export default function ForgotPassword({ csrfToken }) { | ||||||
|   const [loading, setLoading] = React.useState(false); |   const [loading, setLoading] = React.useState(false); | ||||||
|   const [error, setError] = React.useState(null); |   const [error, setError] = React.useState(null); | ||||||
|   const [success, setSuccess] = React.useState(false); |   const [success, setSuccess] = React.useState(false); | ||||||
|  | @ -156,8 +156,17 @@ export default function Page({ csrfToken }) { | ||||||
|   ); |   ); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Page.getInitialProps = async ({ req }) => { | ForgotPassword.getInitialProps = async (context) => { | ||||||
|  |   const { req, res } = context; | ||||||
|  |   const session = await getSession({ req }); | ||||||
|  | 
 | ||||||
|  |   if (session) { | ||||||
|  |     res.writeHead(302, { Location: "/" }); | ||||||
|  |     res.end(); | ||||||
|  |     return; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   return { |   return { | ||||||
|     csrfToken: await getCsrfToken({ req }), |     csrfToken: await getCsrfToken(context), | ||||||
|   }; |   }; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| import Head from "next/head"; | import Head from "next/head"; | ||||||
| import Link from "next/link"; | import Link from "next/link"; | ||||||
| import { getCsrfToken } from "next-auth/client"; | import { getCsrfToken, getSession } from "next-auth/client"; | ||||||
| 
 | 
 | ||||||
| export default function Login({ csrfToken }) { | export default function Login({ csrfToken }) { | ||||||
|   return ( |   return ( | ||||||
|  | @ -79,8 +79,17 @@ export default function Login({ csrfToken }) { | ||||||
|   ); |   ); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Login.getInitialProps = async ({ req }) => { | Login.getInitialProps = async (context) => { | ||||||
|  |   const { req, res } = context; | ||||||
|  |   const session = await getSession({ req }); | ||||||
|  | 
 | ||||||
|  |   if (session) { | ||||||
|  |     res.writeHead(302, { Location: "/" }); | ||||||
|  |     res.end(); | ||||||
|  |     return; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   return { |   return { | ||||||
|     csrfToken: await getCsrfToken({ req }), |     csrfToken: await getCsrfToken(context), | ||||||
|   }; |   }; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | @ -1,6 +1,7 @@ | ||||||
| import Head from "next/head"; | import Head from "next/head"; | ||||||
| import Link from "next/link"; | import Link from "next/link"; | ||||||
| import { CheckIcon } from "@heroicons/react/outline"; | import { CheckIcon } from "@heroicons/react/outline"; | ||||||
|  | import { getSession, signOut } from "next-auth/client"; | ||||||
| 
 | 
 | ||||||
| export default function Logout() { | export default function Logout() { | ||||||
|   return ( |   return ( | ||||||
|  | @ -43,3 +44,14 @@ export default function Logout() { | ||||||
|     </div> |     </div> | ||||||
|   ); |   ); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | Logout.getInitialProps = async (context) => { | ||||||
|  |   const { req } = context; | ||||||
|  |   const session = await getSession({ req }); | ||||||
|  | 
 | ||||||
|  |   if (session) { | ||||||
|  |     signOut({ redirect: false }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   return { session: undefined }; | ||||||
|  | }; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue
	
	 Bailey Pumfleet
						Bailey Pumfleet