This repository was archived by the owner on Jul 15, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +22
-33
lines changed
Expand file tree Collapse file tree 5 files changed +22
-33
lines changed Original file line number Diff line number Diff line change @@ -26,12 +26,13 @@ export default class extends React.Component {
2626 super ( props )
2727 this . state = {
2828 navOpen : false ,
29- modal : false
29+ modal : false ,
30+ providers : null
3031 }
3132 this . toggleModal = this . toggleModal . bind ( this )
3233 }
3334
34- toggleModal ( e ) {
35+ async toggleModal ( e ) {
3536 if ( e ) e . preventDefault ( )
3637
3738 // Save current URL so user is redirected back here after signing in
@@ -41,6 +42,7 @@ export default class extends React.Component {
4142 }
4243
4344 this . setState ( {
45+ providers : this . state . providers || await NextAuth . providers ( ) ,
4446 modal : ! this . state . modal
4547 } )
4648 }
@@ -106,7 +108,7 @@ export default class extends React.Component {
106108 < span className = "ml-2" > © { new Date ( ) . getYear ( ) + 1900 } .</ span >
107109 </ p >
108110 </ Container >
109- < SigninModal modal = { this . state . modal } toggleModal = { this . toggleModal } session = { this . props . session } providers = { this . props . providers } />
111+ < SigninModal modal = { this . state . modal } toggleModal = { this . toggleModal } session = { this . props . session } providers = { this . state . providers } />
110112 </ React . Fragment >
111113 )
112114 }
@@ -244,6 +246,8 @@ export class AdminMenuItem extends React.Component {
244246
245247export class SigninModal extends React . Component {
246248 render ( ) {
249+ if ( this . props . providers === null ) return null
250+
247251 return (
248252 < Modal isOpen = { this . props . modal } toggle = { this . props . toggleModal } style = { { maxWidth : 700 } } >
249253 < ModalHeader > Sign up / Sign in</ ModalHeader >
Original file line number Diff line number Diff line change @@ -3,22 +3,11 @@ import Layout from '../components/layout'
33import { NextAuth } from 'next-auth-client'
44
55export default class extends React . Component {
6- // Expose session to all pages
6+
77 static async getInitialProps ( { req} ) {
8- // Export this.props.session to all pages
9- const session = await NextAuth . init ( { req} )
10-
11- // If the user is not already signed in, get currently configured providers.
12- // This project uses them in the sign in dialog, which is embedded on all
13- // pages when browsing the site but not signed in.
14- //
15- // NB: As an improvement, this could alternatively be called on modal load.
16- const providers = ( ! session . user ) ? await NextAuth . providers ( { req} ) : { }
17-
188 return {
19- session : session ,
20- providers : providers ,
21- lang : 'en' // Sets a lang property for accessibility
9+ session : await NextAuth . init ( { req} ) , // Add this.props.session to all pages
10+ lang : 'en' // Add a lang property for accessibility
2211 }
2312 }
2413
Original file line number Diff line number Diff line change 11{
22 "name" : " nextjs-starter" ,
3- "version" : " 4.5.1 " ,
3+ "version" : " 4.5.2 " ,
44 "description" : " A starter Next.js project with email and oAuth authentication" ,
55 "author" :
" Iain Collins <[email protected] >" ,
66 "license" : " ISC" ,
Original file line number Diff line number Diff line change @@ -7,7 +7,8 @@ import { NextAuth } from 'next-auth-client'
77export default class extends Page {
88
99 static async getInitialProps ( { req, res, query} ) {
10- const session = await NextAuth . init ( { force : true , req : req } )
10+ let props = await super . getInitialProps ( { req} )
11+ props . session = await NextAuth . init ( { force : true , req : req } )
1112
1213 // If signed in already, instead of displaying message send to callback page
1314 // which should redirect them to whatever page it normally sends clients to
@@ -19,11 +20,9 @@ export default class extends Page {
1920 }
2021 }
2122
22- return {
23- session : session ,
24- providers : await NextAuth . providers ( { req} ) ,
25- email : query . email
26- }
23+ props . email = query . email
24+
25+ return props
2726 }
2827
2928 render ( ) {
Original file line number Diff line number Diff line change @@ -12,11 +12,12 @@ import SignIn from '../../components/signin'
1212export default class extends Page {
1313
1414 static async getInitialProps ( { req, res, query} ) {
15- const session = await NextAuth . init ( { force : true , req : req } )
16- const providers = await NextAuth . providers ( { req} )
17-
15+ let props = await super . getInitialProps ( { req} )
16+ props . session = await NextAuth . init ( { force : true , req : req } )
17+ props . providers = await NextAuth . providers ( { req} )
18+
1819 // If signed in already, redirect to account management page.
19- if ( session . user ) {
20+ if ( props . session . user ) {
2021 if ( req ) {
2122 res . redirect ( '/account' )
2223 } else {
@@ -30,11 +31,7 @@ export default class extends Page {
3031 cookies . set ( 'redirect_url' , query . redirect , { path : '/' } )
3132 }
3233
33- return {
34- session : session ,
35- providers : providers ,
36- providers : await NextAuth . providers ( { req} )
37- }
34+ return props
3835 }
3936
4037 render ( ) {
You can’t perform that action at this time.
0 commit comments