generated from cp-20/nextjs-with-mantine-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
334 additions
and
304 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,13 @@ | ||
import type { Context } from 'hono'; | ||
import { getCookie } from 'hono/cookie'; | ||
import { z } from 'zod'; | ||
|
||
import { SUPABASE_ID } from '@/utils/env'; | ||
import { supabase } from '@/gateways/supabase'; | ||
import type { SupabaseMiddlewareVariable } from '@/middleware/supabase'; | ||
|
||
const authCookieSchema = z.tuple([ | ||
z.string(), | ||
z.string(), | ||
z.string().nullable(), | ||
z.string().nullable(), | ||
z.unknown(), | ||
]); | ||
export const getUser = async ( | ||
c: Context<{ Variables: SupabaseMiddlewareVariable }>, | ||
) => { | ||
const { | ||
data: { user }, | ||
} = await c.var.supabase.auth.getUser(); | ||
|
||
export const getUser = async (c: Context) => { | ||
const authCookie = getCookie(c, `sb-${SUPABASE_ID}-auth-token`); | ||
|
||
if (authCookie === undefined) return null; | ||
|
||
try { | ||
const authCookieParts = authCookieSchema.parse(JSON.parse(authCookie)); | ||
const [accessToken, refreshToken, _providerToken, _providerRefreshToken] = | ||
authCookieParts; | ||
|
||
const result = await supabase.auth.setSession({ | ||
access_token: accessToken, | ||
refresh_token: refreshToken, | ||
}); | ||
|
||
if (result.error !== null) return null; | ||
|
||
return result.data.user; | ||
} catch (err) { | ||
return null; | ||
} | ||
return user; | ||
}; |
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,37 @@ | ||
import { createServerClient } from '@supabase/ssr'; | ||
import type { SupabaseClient } from '@supabase/supabase-js'; | ||
import type { Context, MiddlewareHandler } from 'hono'; | ||
import { deleteCookie, getCookie, setCookie } from 'hono/cookie'; | ||
|
||
import { SUPABASE_ANON_KEY, SUPABASE_URL } from '@/utils/env'; | ||
|
||
export interface SupabaseMiddlewareVariable { | ||
supabase: SupabaseClient; | ||
[key: string]: unknown; | ||
} | ||
|
||
export const supabaseMiddleware: MiddlewareHandler<{ | ||
Variables: SupabaseMiddlewareVariable; | ||
}> = async (c, next) => { | ||
const client = createServerClient(SUPABASE_URL, SUPABASE_ANON_KEY, { | ||
cookies: { | ||
get: (key) => { | ||
return getCookie(c as Context, key); | ||
}, | ||
set: (key, value, options) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- 知らんけど | ||
setCookie(c, key, value, options); | ||
}, | ||
remove: (key, options) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- 知らんけど | ||
deleteCookie(c, key, options); | ||
}, | ||
}, | ||
cookieOptions: { | ||
httpOnly: true, | ||
secure: true, | ||
}, | ||
}); | ||
c.set('supabase', client); | ||
await next(); | ||
}; |
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
Oops, something went wrong.