How to use next-auth - 10 common examples

To help you get started, we’ve selected a few next-auth examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github datavis-tech / vizhub-legacy / packages / web / pages / auth / index.js View on Github external
static async getInitialProps({req, res, query}) {
    const props = await super.getInitialProps({req});
    const session = await NextAuth.init({force: true, req: req});
    props.user = userFromSession(session);
    props.providers = await NextAuth.providers({req});
    
    // If signed in already, redirect to account management page.
    if (props.user.authenticated) {
      if (req) {
        res.redirect('/account');
      } else {
        Router.push('/account');
      }
    }

    // If passed a redirect parameter, save it as a cookie
    if (query.redirect) {
      const cookies = new Cookies((req && req.headers.cookie) ? req.headers.cookie : null);
      cookies.set('redirect_url', query.redirect, { path: '/' });
github iaincollins / nextjs-starter / pages / auth / check-email.js View on Github external
static async getInitialProps({req, res, query}) {
    let props = await super.getInitialProps({req})
    props.session = await NextAuth.init({force: true, req: req})
    
    // If signed in already, instead of displaying message send to callback page
    // which should redirect them to whatever page it normally sends clients to
    if (props.session.user) {
      if (req) {
        res.redirect('/auth/callback')
      } else {
        Router.push('/auth/callback')
      }
    }
      
    props.email = query.email
    
    return props
  }
github datavis-tech / vizhub-legacy / packages / web / pages / account.js View on Github external
async componentDidMount() {
    const session = await NextAuth.init({ force: true })

    this.setState({
      user: userFromSession(session),
      csrfToken: session.csrfToken
    })

    // If the user bounces off to link/unlink their account we want them to
    // land back here after signing in with the other service / unlinking.
    const cookies = new Cookies()
    cookies.set('redirect_url', window.location.pathname, { path: '/' })
  }
github datavis-tech / vizhub-legacy / packages / web / pages / auth / callback.js View on Github external
async componentDidMount() {
    // Get latest session data after rendering on client *then* redirect.
    // The ensures client state is always updated after signing in or out.
    // (That's why we use a callback page)
    const session = await NextAuth.init({force: true})
    Router.push(this.props.redirectTo || '/')
  }
github datavis-tech / vizhub-legacy / packages / web / pages / auth / callback.js View on Github external
static async getInitialProps({req}) {
    const session = await NextAuth.init({force: true, req: req})

    const cookies = new Cookies((req && req.headers.cookie) ? req.headers.cookie : null)
    
    // If the user is signed in, we look for a redirect URL cookie and send 
    // them to that page, so that people signing in end up back on the page they
    // were on before signing in. Defaults to '/'.
    let redirectTo = '/'
    if (session.user) {

      // Read redirect URL to redirect to from cookies
      redirectTo = cookies.get('redirect_url') || redirectTo
      
      // Allow relative paths only - strip protocol/host/port if they exist.
      redirectTo = redirectTo.replace( /^[a-zA-Z]{3,5}\:\/{2}[a-zA-Z0-9_.:-]+\//, '')
    }
github iaincollins / nextjs-starter / pages / auth / index.js View on Github external
static async getInitialProps({req, res, query}) {
    let props = await super.getInitialProps({req})
    props.session = await NextAuth.init({force: true, req: req})
    props.providers = await NextAuth.providers({req})
    
    // If signed in already, redirect to account management page.
    if (props.session.user) {
      if (req) {
        res.redirect('/account')
      } else {
        Router.push('/account')
      }
    }

    // If passed a redirect parameter, save it as a cookie
    if (query.redirect) {
      const cookies = new Cookies((req && req.headers.cookie) ? req.headers.cookie : null)
      cookies.set('redirect_url', query.redirect, { path: '/' })
    }
github iaincollins / nextjs-starter / pages / auth / index.js View on Github external
static async getInitialProps({req, res, query}) {
    let props = await super.getInitialProps({req})
    props.session = await NextAuth.init({force: true, req: req})
    props.providers = await NextAuth.providers({req})
    
    // If signed in already, redirect to account management page.
    if (props.session.user) {
      if (req) {
        res.redirect('/account')
      } else {
        Router.push('/account')
      }
    }

    // If passed a redirect parameter, save it as a cookie
    if (query.redirect) {
      const cookies = new Cookies((req && req.headers.cookie) ? req.headers.cookie : null)
      cookies.set('redirect_url', query.redirect, { path: '/' })
    }
github datavis-tech / vizhub-legacy / packages / web / pages / auth / index.js View on Github external
static async getInitialProps({req, res, query}) {
    const props = await super.getInitialProps({req});
    const session = await NextAuth.init({force: true, req: req});
    props.user = userFromSession(session);
    props.providers = await NextAuth.providers({req});
    
    // If signed in already, redirect to account management page.
    if (props.user.authenticated) {
      if (req) {
        res.redirect('/account');
      } else {
        Router.push('/account');
      }
    }

    // If passed a redirect parameter, save it as a cookie
    if (query.redirect) {
      const cookies = new Cookies((req && req.headers.cookie) ? req.headers.cookie : null);
      cookies.set('redirect_url', query.redirect, { path: '/' });
    }
github iaincollins / next-auth / example / pages / auth / credentials.js View on Github external
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.")
    })
  }
github iaincollins / nextjs-starter / components / signin.js View on Github external
handleSubmit(event) {
    event.preventDefault()
    
    if (!this.state.email) return

    this.setState({
      submitting: true
    })
    
    // Save current URL so user is redirected back here after signing in
    const cookies = new Cookies()
    cookies.set('redirect_url', window.location.pathname, { path: '/' })

    NextAuth.signin(this.state.email)
    .then(() => {
      Router.push(`/auth/check-email?email=${this.state.email}`)
    })
    .catch(err => {
      Router.push(`/auth/error?action=signin&type=email&email=${this.state.email}`)
    })
  }