-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from auth0/feature/frontend-hook
Add frontend hook
- Loading branch information
Showing
43 changed files
with
285 additions
and
7,473 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
], | ||
"env": { | ||
"node": true, | ||
"browser": true, | ||
"jest": true | ||
}, | ||
"settings": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import { useUser } from '@auth0/nextjs-auth0'; | ||
|
||
import auth0 from '../lib/auth0'; | ||
import { fetchUser } from '../lib/user'; | ||
import createLoginUrl from '../lib/url-helper'; | ||
import RedirectToLogin from '../components/login-redirect'; | ||
|
||
export default function withAuth(InnerComponent) { | ||
return class Authenticated extends Component { | ||
static async getInitialProps(ctx) { | ||
if (!ctx.req) { | ||
const user = await fetchUser(); | ||
return { | ||
user | ||
}; | ||
} | ||
|
||
const session = await auth0.getSession(ctx.req, ctx.res); | ||
if (!session || !session.user) { | ||
ctx.res.writeHead(302, { | ||
Location: createLoginUrl(ctx.req.url) | ||
}); | ||
ctx.res.end(); | ||
return; | ||
} | ||
|
||
return { user: session.user }; | ||
const Authenticated = (props) => { | ||
const { user } = useUser(); | ||
|
||
if (!user) { | ||
return <RedirectToLogin />; // do you need a "redirecting to login" route? | ||
} | ||
|
||
constructor(props) { | ||
super(props); | ||
return <InnerComponent {...props} user={user} />; | ||
}; | ||
|
||
Authenticated.getInitialProps = async (ctx) => { | ||
if (!ctx.req) { | ||
const response = await fetch('/api/me'); | ||
const result = response.ok ? await response.json() : null; | ||
|
||
return { user: result }; | ||
} | ||
|
||
render() { | ||
if (!this.props.user) { | ||
return <RedirectToLogin />; | ||
} | ||
const session = await auth0.getSession(ctx.req, ctx.res); | ||
|
||
return <div>{<InnerComponent {...this.props} user={this.props.user} />}</div>; | ||
if (!session || !session.user) { | ||
ctx.res.writeHead(302, { | ||
Location: createLoginUrl(ctx.req.url) | ||
}); | ||
ctx.res.end(); | ||
|
||
return; | ||
} | ||
|
||
return { user: session.user }; | ||
}; | ||
|
||
return Authenticated; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
import { UserProvider } from '@auth0/nextjs-auth0'; | ||
|
||
export default function App({ Component, pageProps }) { | ||
// If you've used `withAuth`, pageProps.user can pre-populate the hook | ||
// if you haven't used `withAuth`, pageProps.user is undefined so the hook | ||
// fetches the user from the API routes | ||
const { user, ...otherProps } = pageProps; | ||
|
||
return ( | ||
<UserProvider user={user}> | ||
<Component {...otherProps} /> | ||
</UserProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import { useUser } from '@auth0/nextjs-auth0'; | ||
|
||
import auth0 from '../lib/auth0'; | ||
import { fetchUser } from '../lib/user'; | ||
import createLoginUrl from '../lib/url-helper'; | ||
import RedirectToLogin from '../components/login-redirect'; | ||
|
||
export default function withAuth(InnerComponent) { | ||
return class Authenticated extends Component { | ||
static async getInitialProps(ctx) { | ||
if (!ctx.req) { | ||
const user = await fetchUser(); | ||
return { | ||
user | ||
}; | ||
} | ||
|
||
const session = await auth0.getSession(ctx.req, ctx.res); | ||
if (!session || !session.user) { | ||
ctx.res.writeHead(302, { | ||
Location: createLoginUrl(ctx.req.url) | ||
}); | ||
ctx.res.end(); | ||
return; | ||
} | ||
|
||
return { user: session.user }; | ||
const Authenticated = (props) => { | ||
const { user } = useUser(); | ||
|
||
if (!user) { | ||
return <RedirectToLogin />; // do you need a "redirecting to login" route? | ||
} | ||
|
||
constructor(props) { | ||
super(props); | ||
return <InnerComponent {...props} user={user} />; | ||
}; | ||
|
||
Authenticated.getInitialProps = async (ctx) => { | ||
if (!ctx.req) { | ||
const response = await fetch('/api/me'); | ||
const result = response.ok ? await response.json() : null; | ||
|
||
return { user: result }; | ||
} | ||
|
||
render() { | ||
if (!this.props.user) { | ||
return <RedirectToLogin />; | ||
} | ||
const session = await auth0.getSession(ctx.req, ctx.res); | ||
|
||
return <div>{<InnerComponent {...this.props} user={this.props.user} />}</div>; | ||
if (!session || !session.user) { | ||
ctx.res.writeHead(302, { | ||
Location: createLoginUrl(ctx.req.url) | ||
}); | ||
ctx.res.end(); | ||
|
||
return; | ||
} | ||
|
||
return { user: session.user }; | ||
}; | ||
|
||
return Authenticated; | ||
} |
Oops, something went wrong.