generated from t3-oss/create-t3-turbo
-
-
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
1 parent
6f409c7
commit f93ff71
Showing
15 changed files
with
790 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# These are supported funding model platforms | ||
|
||
github: juliusmarminge | ||
github: trevorpfiz |
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,48 @@ | ||
import * as Linking from "expo-linking"; | ||
import * as Browser from "expo-web-browser"; | ||
|
||
import { api } from "./api"; | ||
import { deleteToken, setToken } from "./session-store"; | ||
|
||
export const signIn = async () => { | ||
const signInUrl = "http://localhost:3000/api/auth/signin"; | ||
const redirectTo = "exp://192.168.10.181:8081/login"; | ||
const result = await Browser.openAuthSessionAsync( | ||
`${signInUrl}?expo-redirect=${encodeURIComponent(redirectTo)}`, | ||
redirectTo, | ||
); | ||
if (result.type !== "success") return; | ||
|
||
const url = Linking.parse(result.url); | ||
const sessionToken = String(url.queryParams?.session_token); | ||
if (!sessionToken) return; | ||
|
||
await setToken(sessionToken); | ||
// ... | ||
}; | ||
|
||
export const useUser = () => { | ||
const { data: session } = api.auth.getSession.useQuery(); | ||
return session?.user ?? null; | ||
}; | ||
|
||
export const useSignIn = () => { | ||
const utils = api.useUtils(); | ||
|
||
return async () => { | ||
await signIn(); | ||
await utils.invalidate(); | ||
}; | ||
}; | ||
|
||
export const useSignOut = () => { | ||
const utils = api.useUtils(); | ||
const signOut = api.auth.signOut.useMutation(); | ||
|
||
return async () => { | ||
const res = await signOut.mutateAsync(); | ||
if (!res.success) return; | ||
await deleteToken(); | ||
await utils.invalidate(); | ||
}; | ||
}; |
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,6 @@ | ||
import * as SecureStore from "expo-secure-store"; | ||
|
||
const key = "session_token"; | ||
export const getToken = () => SecureStore.getItemAsync(key); | ||
export const deleteToken = () => SecureStore.deleteItemAsync(key); | ||
export const setToken = (v: string) => SecureStore.setItemAsync(key, v); |
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,48 @@ | ||
export { GET, POST } from "@acme/auth"; | ||
import { cookies } from "next/headers"; | ||
import { NextResponse } from "next/server"; | ||
import type { NextRequest } from "next/server"; | ||
|
||
import { GET as _GET, POST } from "@acme/auth"; | ||
|
||
export const runtime = "edge"; | ||
|
||
const expoRedirectCookieName = "__acme-expo-redirect-state"; | ||
const setCookieMatchPattern = /next-auth\.session-token=([^;]+)/; | ||
|
||
export const GET = async ( | ||
req: NextRequest, | ||
props: { params: { nextauth: string[] } }, | ||
) => { | ||
const nextauthAction = props.params.nextauth[0]; | ||
const isExpoSignIn = req.nextUrl.searchParams.get("expo-redirect"); | ||
const isExpoCallback = cookies().get(expoRedirectCookieName); | ||
|
||
if (nextauthAction === "signin" && !!isExpoSignIn) { | ||
// set a cookie we can read in the callback | ||
// to know to send the user back to expo | ||
cookies().set({ | ||
name: expoRedirectCookieName, | ||
value: isExpoSignIn, | ||
maxAge: 60 * 10, // 10 min | ||
path: "/", | ||
}); | ||
} | ||
|
||
if (nextauthAction === "callback" && !!isExpoCallback) { | ||
cookies().delete(expoRedirectCookieName); | ||
|
||
const authResponse = await _GET(req); | ||
const setCookie = authResponse.headers.getSetCookie()[0]; | ||
const match = setCookie?.match(setCookieMatchPattern)?.[1]; | ||
if (!match) throw new Error("No session cookie found"); | ||
|
||
const url = new URL(isExpoCallback.value); | ||
url.searchParams.set("session_token", match); | ||
return NextResponse.redirect(url); | ||
} | ||
|
||
// Every other request just calls the default handler | ||
return _GET(req); | ||
}; | ||
|
||
export { POST }; |
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
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.