mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
* All dependancies updated, including the example to include latest release of Next.js and React. * Includes fix for Keycloak strategy support and improved oAuth strategy compatibility. * Includes enhancement for exposing additional parameters when calling getProfile(). * `sessionResave` now defaults to `true`, which ensures sessions always rotate properly. This can be disabled for special use cases - it does not work well with some Express Session Stores, but for most people it should be fine and should rotate sessions correctly. If set to `false` user sessions are likely to expire prematurely. It is strongly recommended you do not change this from the default setting. As a side effect, this will case all sessions - including anonymous sessions created when users have connected but not logged in yet - to be saved as a session in the store. This is because the current Cross Site Request Forgery option associates a token with a session in the browser. If you want to avoid creating anonymous sessions, set `csrf` to `false` to disable CSRF protection; sessions will then only be created when a user signs in. A future update should include the option to implement CSRF using the Double Submit Cookie method so it can be enabled without causing this side effect.
109 lines
3.6 KiB
JavaScript
109 lines
3.6 KiB
JavaScript
import React from 'react'
|
|
import Router from 'next/router'
|
|
import Link from 'next/link'
|
|
import { NextAuth } from 'next-auth/client'
|
|
|
|
export default class extends React.Component {
|
|
|
|
static async getInitialProps({req}) {
|
|
return {
|
|
session: await NextAuth.init({req}),
|
|
linkedAccounts: await NextAuth.linked({req}),
|
|
providers: await NextAuth.providers({req})
|
|
}
|
|
}
|
|
|
|
constructor(props) {
|
|
super(props)
|
|
this.state = {
|
|
email: '',
|
|
password: '',
|
|
session: this.props.session
|
|
}
|
|
this.handleEmailChange = this.handleEmailChange.bind(this)
|
|
this.handlePasswordChange = this.handlePasswordChange.bind(this)
|
|
this.handleSignInSubmit = this.handleSignInSubmit.bind(this)
|
|
}
|
|
|
|
async componentDidMount() {
|
|
if (this.props.session.user) {
|
|
Router.push(`/auth/`)
|
|
}
|
|
}
|
|
|
|
handleEmailChange(event) {
|
|
this.setState({
|
|
email: event.target.value
|
|
})
|
|
}
|
|
|
|
handlePasswordChange(event) {
|
|
this.setState({
|
|
password: event.target.value
|
|
})
|
|
}
|
|
|
|
handleSignInSubmit(event) {
|
|
event.preventDefault()
|
|
|
|
// An object passed NextAuth.signin will be passed to your signin() function
|
|
NextAuth.signin({
|
|
email: this.state.email,
|
|
password: this.state.password
|
|
})
|
|
.then(authenticated => {
|
|
Router.push(`/auth/callback`)
|
|
})
|
|
.catch(() => {
|
|
alert("Authentication failed.")
|
|
})
|
|
}
|
|
|
|
render() {
|
|
if (this.props.session.user) {
|
|
return null
|
|
} else {
|
|
return (
|
|
<div className="container">
|
|
<div className="text-center">
|
|
<h1 className="display-5 mt-4 mb-2">NextAuth - Custom Sign In</h1>
|
|
</div>
|
|
<div className="row">
|
|
<div className="col-sm-12 col-md-10 col-lg-8 col-xl-7 mr-auto ml-auto">
|
|
<p className="mt-3 mb-4 text-center">
|
|
If you want to support password based sign in, two factor authentication
|
|
or another sign in method, define a signin() function
|
|
in <strong>next-auth.functions.js</strong>.
|
|
</p>
|
|
<div className="card mt-3 mb-3">
|
|
<h4 className="card-header">Sign In</h4>
|
|
<div className="card-body pb-0">
|
|
<form id="signin" method="post" action="/auth/signin" onSubmit={this.handleSignInSubmit}>
|
|
<input name="_csrf" type="hidden" value={this.state.session.csrfToken}/>
|
|
<p>
|
|
<label htmlFor="email">Email address</label><br/>
|
|
<input name="email" type="text" placeholder="j.smith@example.com" id="email" className="form-control" value={this.state.email} onChange={this.handleEmailChange}/>
|
|
</p>
|
|
<p>
|
|
<label htmlFor="password">Password</label><br/>
|
|
<input name="password" type="password" placeholder="" id="password" className="form-control" value={this.state.password} onChange={this.handlePasswordChange}/>
|
|
</p>
|
|
<p className="text-right">
|
|
<button id="submitButton" type="submit" className="btn btn-outline-primary">Sign in</button>
|
|
</p>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<p className="text-italic text-muted text-center small">
|
|
For this to work, you will need enable the signin() function in <strong>next-auth.functions.js</strong>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<p className="text-center">
|
|
<Link href="/auth"><a>Back</a></Link>
|
|
</p>
|
|
</div>
|
|
)
|
|
}
|
|
}
|
|
} |