Skip to content

Instantly share code, notes, and snippets.

@alfianyusufabdullah
Created February 20, 2023 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfianyusufabdullah/3a2611d0db9324029fafa920583055c1 to your computer and use it in GitHub Desktop.
Save alfianyusufabdullah/3a2611d0db9324029fafa920583055c1 to your computer and use it in GitHub Desktop.
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'

export default NextAuth({
  providers: [
    Providers.Strava({
      clientId: process.env.STRAVA_CLIENT_ID,
      clientSecret: process.env.STRAVA_CLIENT_SECRET,
      authorize: async (credentials) => {
        // Exchange authorization code for access token
        const response = await fetch('https://www.strava.com/oauth/token', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            client_id: process.env.STRAVA_CLIENT_ID,
            client_secret: process.env.STRAVA_CLIENT_SECRET,
            code: credentials.authorizationCode,
          }),
        })

        const data = await response.json()

        if (!response.ok) {
          throw new Error(data.message)
        }

        return {
          accessToken: data.access_token,
          refreshToken: data.refresh_token,
          expiresIn: data.expires_in,
        }
      },
    }),
  ],
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment