-
-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add logout api route * chore: update changelog. * feat: logout returnTo in HandleAuthOptions.
- Loading branch information
1 parent
4a980a2
commit ad3c71a
Showing
12 changed files
with
84 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { handleAuth } from '@supabase/supabase-auth-helpers/nextjs'; | ||
|
||
export default handleAuth(); | ||
export default handleAuth({ logout: { returnTo: '/' } }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { CookieOptions } from '../types'; | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { setCookies } from '../../shared/utils/cookies'; | ||
import { | ||
NextRequestAdapter, | ||
NextResponseAdapter | ||
} from '../../shared/adapters/NextAdapter'; | ||
import { supabaseClient } from '../utils/initSupabase'; | ||
import { COOKIE_OPTIONS } from '../../shared/utils/constants'; | ||
|
||
export interface HandleLogoutOptions { | ||
cookieOptions?: CookieOptions; | ||
returnTo?: string; | ||
} | ||
|
||
export default function handleLogout( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
options: HandleLogoutOptions = {} | ||
) { | ||
let { returnTo } = req.query; | ||
if (!returnTo) returnTo = options?.returnTo ?? '/'; | ||
returnTo = Array.isArray(returnTo) ? returnTo[0] : returnTo; | ||
returnTo = returnTo.charAt(0) === '/' ? returnTo : `/${returnTo}`; | ||
const { cookieOptions = COOKIE_OPTIONS } = options; | ||
|
||
// Logout request to Gotrue | ||
const access_token = req.cookies[`${cookieOptions.name}-access-token`]; | ||
if (access_token) supabaseClient.auth.api.signOut(access_token); | ||
|
||
// Delete cookies | ||
setCookies( | ||
new NextRequestAdapter(req), | ||
new NextResponseAdapter(res), | ||
['access-token', 'refresh-token'].map((key) => ({ | ||
name: `${cookieOptions.name}-${key}`, | ||
value: '', | ||
maxAge: -1 | ||
})) | ||
); | ||
|
||
res.redirect(returnTo as string); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.