How to use the next-auth/client.NextAuth.init function in next-auth

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 / next-auth / example / pages / index.js View on Github external
static async getInitialProps({req}) {
    return {
      session: await NextAuth.init({req})
    }
  }
github iaincollins / next-auth / example / pages / auth / callback.js View on Github external
static async getInitialProps({req}) {
    return {
      session: await NextAuth.init({force: true, req: req})
    }
  }
github datavis-tech / vizhub-legacy / packages / web / components / page.js View on Github external
static async getInitialProps({req, query}) {
    const session = await NextAuth.init({req});
    return {
      user: userFromSession(session),
      csrfToken: session.csrfToken,
      lang: 'en',
      query
    };
  }
}
github iaincollins / next-auth / example / pages / auth / credentials.js View on Github external
static async getInitialProps({req}) {
    return {
      session: await NextAuth.init({req}),
      linkedAccounts: await NextAuth.linked({req}),
      providers: await NextAuth.providers({req})
    }
  }