Skip to content

Commit

Permalink
fix(next-auth): fix next auth to use new sdk updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bahdcoder committed Oct 25, 2021
1 parent 5d6843e commit 5c5275a
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 35 deletions.
22 changes: 11 additions & 11 deletions packages/next-auth/src/frontend/use-auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, {
useEffect,
useState,
useContext,
useRef,
useRef
} from 'react'

import { getLoginUrl } from '../utils'
Expand All @@ -14,8 +14,8 @@ const UserContextDefaultData = {
setAuth() {},
logout() {},
config: {
loginPath: getLoginUrl(),
},
loginPath: getLoginUrl()
}
}

interface AuthConfig {
Expand All @@ -25,7 +25,7 @@ interface AuthConfig {
interface UserContextData {
// @ts-ignore These types will be available after the tensei-sdk generate command is run by the developer.
user?: import('@tensei/sdk').User
expires_in?: number
expiresIn?: number
accessToken?: string
loading: boolean
setAuth: (auth: UserContextData) => void
Expand All @@ -45,13 +45,13 @@ export const UserProvider: FunctionComponent = ({ children }) => {

const checkSession = () => {
fetch('/api/auth/check-session')
.then((response) => response.json())
.then((data) => {
.then(response => response.json())
.then(data => {
// Set an interval to dynamically call the API and call /api/auth/check-session

setAuth({
...auth,
...data,
...data
})
setLoading(false)
})
Expand All @@ -63,16 +63,16 @@ export const UserProvider: FunctionComponent = ({ children }) => {
setAuth({
...auth,
accessToken: undefined,
expires_in: undefined,
user: undefined,
expiresIn: undefined,
user: undefined
})
}

useEffect(() => {
checkSession()
}, [])

const delay = auth?.expires_in ? (auth.expires_in - 10) * 1000 : 60 * 14 * 1000
const delay = auth?.expiresIn ? (auth.expiresIn - 10) * 1000 : 60 * 14 * 1000

useInterval(
() => {
Expand All @@ -89,7 +89,7 @@ export const UserProvider: FunctionComponent = ({ children }) => {
...auth,
loading,
setAuth,
logout,
logout
}}
>
{children}
Expand Down
3 changes: 0 additions & 3 deletions packages/next-auth/src/handlers/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { withIronSession } from 'next-iron-session'
import { NextApiHandler, NextApiResponse } from 'next'

import { NextIronHandler, wrapErrorHandling, withSession } from '../utils'

import handleLogin from './login'
Expand Down
6 changes: 3 additions & 3 deletions packages/next-auth/src/handlers/check-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ export default async function handleCheckSession(
) {
const auth = request.session.get('auth')

if (!auth || !auth.refresh_token) {
if (!auth || !auth.refreshToken) {
return response.status(200).json({})
}

let apiResponse

try {
apiResponse = await tensei.auth().refreshToken({
token: auth.refresh_token
token: auth.refreshToken
})
} catch (error) {
// Terminate the existing session.
Expand All @@ -30,7 +30,7 @@ export default async function handleCheckSession(
}

request.session.set('auth', {
refresh_token: apiResponse.data.data.refresh_token,
refreshToken: apiResponse.data.data.refreshToken,
accessTokenExpiresAt: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expiresIn
)
Expand Down
2 changes: 1 addition & 1 deletion packages/next-auth/src/handlers/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default async function handleLogin(
})

request.session.set('auth', {
refresh_token: apiResponse.data.data.refresh_token,
refreshToken: apiResponse.data.data.refreshToken,
accessTokenExpiresAt: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expiresIn
)
Expand Down
2 changes: 1 addition & 1 deletion packages/next-auth/src/handlers/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default async function handleSignup(
})

request.session.set('auth', {
refresh_token: apiResponse.data.data.refresh_token,
refreshToken: apiResponse.data.data.refreshToken,
accessTokenExpiresAt: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expiresIn
)
Expand Down
2 changes: 1 addition & 1 deletion packages/next-auth/src/handlers/social-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default async function handleSocialCallback(
})

request.session.set('auth', {
refreshToken: apiResponse.data.data.refresh_token,
refreshToken: apiResponse.data.data.refreshToken,
accessTokenExpiresAt: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expiresIn
)
Expand Down
2 changes: 1 addition & 1 deletion packages/next-auth/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type NextIronHandler = (
) => void | Promise<void>

export const prepareAuthData = (payload: any) => {
const { refresh_token, ...rest } = payload
const { refreshToken, ...rest } = payload

return rest
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class AuthAPI {

this.auth_response = {
accessToken: session.accessToken,
expires_in: session.expiresIn
expiresIn: session.expiresIn
} as any

this.updateUser(response.data.data)
Expand Down
26 changes: 13 additions & 13 deletions packages/sdk/tests/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ test('can login with email and password authentication', async () => {
data: {
data: {
accessToken: 'accessToken',
refresh_token: 'REFRESH_TOKEN',
expires_in: 60
refreshToken: 'REFRESH_TOKEN',
expiresIn: 60
}
}
}
Expand Down Expand Up @@ -44,9 +44,9 @@ test('can login with email and password authentication', async () => {
const storage = new LocalStorageStore('___tensei__session___')

expect(storage.get()).toEqual({
access_token_expires_in: responsePayload.data.data.expires_in,
refresh_token: responsePayload.data.data.refresh_token,
current_time: expect.any(String)
accessTokenExpiresIn: responsePayload.data.data.expiresIn,
refreshToken: responsePayload.data.data.refreshToken,
currentTime: expect.any(String)
})
})

Expand All @@ -57,13 +57,13 @@ test('can login with access tokens', async () => {
data: {
data: {
accessToken: 'accessToken',
refresh_token: 'REFRESH_TOKEN',
refreshToken: 'REFRESH_TOKEN',
user: {
id: 1,
email: 'hey@tenseijs.com',
name: 'Hey Tensei'
},
expires_in: 60
expiresIn: 60
}
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ test('can login with access tokens', async () => {
expect(storage.get()).toEqual({
currentTime: expect.any(String),
accessTokenExpiresAt: expect.any(String),
expiresIn: responsePayload.data.data.expires_in,
expiresIn: responsePayload.data.data.expiresIn,
accessToken: responsePayload.data.data.accessToken
})
})
Expand All @@ -110,8 +110,8 @@ test('can register with email and password authentication', async () => {
data: {
data: {
accessToken: 'accessToken',
refresh_token: 'REFRESH_TOKEN',
expires_in: 60
refreshToken: 'REFRESH_TOKEN',
expiresIn: 60
}
}
}
Expand Down Expand Up @@ -146,9 +146,9 @@ test('can register with email and password authentication', async () => {
const storage = new LocalStorageStore('___tensei__session___')

expect(storage.get()).toEqual({
access_token_expires_in: responsePayload.data.data.expires_in,
refresh_token: responsePayload.data.data.refresh_token,
current_time: expect.any(String)
accessTokenExpiresIn: responsePayload.data.data.expiresIn,
refreshToken: responsePayload.data.data.refreshToken,
currentTime: expect.any(String)
})
})

Expand Down

0 comments on commit 5c5275a

Please sign in to comment.