-
-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(wip): implement next-auth for expo #133
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,70 @@ | ||
import React from "react"; | ||
import { Text, View, SafeAreaView, TouchableOpacity } from "react-native"; | ||
// import { SafeAreaView } from "react-native-safe-area-context"; | ||
import * as AppleAuth from "expo-apple-authentication"; | ||
import { signIn } from "@acme/auth/src/expo/client"; | ||
import { FontAwesome5 } from "@expo/vector-icons"; | ||
import { useSession } from "next-auth/expo"; | ||
import { nativeApple, socialLogin } from "../utils/auth"; | ||
|
||
export const AuthScreen = () => { | ||
const session = useSession(); | ||
|
||
return ( | ||
<SafeAreaView className="bg-[#2e026d] bg-gradient-to-b from-[#2e026d] to-[#15162c]"> | ||
<View className="h-full w-full p-4"> | ||
<Text className="mx-auto pb-2 text-5xl font-bold text-white"> | ||
Create <Text className="text-[#cc66ff]">T3</Text> Turbo | ||
</Text> | ||
|
||
<View className="flex flex-col items-center gap-4 p-2"> | ||
<Text className="mx-auto text-3xl font-bold text-white"> | ||
Authenticate yourself! | ||
</Text> | ||
<AppleAuth.AppleAuthenticationButton | ||
buttonType={AppleAuth.AppleAuthenticationButtonType.SIGN_IN} | ||
buttonStyle={AppleAuth.AppleAuthenticationButtonStyle.BLACK} | ||
cornerRadius={5} | ||
className="h-12 w-full" | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
onPress={async () => { | ||
await signIn(nativeApple); | ||
}} | ||
/> | ||
<TouchableOpacity | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
onPress={async () => { | ||
await signIn(() => socialLogin("github")); | ||
}} | ||
className="flex h-12 w-full flex-row items-center justify-center space-x-2 rounded-md bg-black text-white" | ||
> | ||
<FontAwesome5 name="github" size={16} color="white" /> | ||
<Text className="text-lg font-semibold text-white"> | ||
Sign In With GitHub | ||
</Text> | ||
</TouchableOpacity> | ||
<TouchableOpacity | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
onPress={async () => { | ||
await signIn(() => socialLogin("discord")); | ||
}} | ||
className="flex h-12 w-full flex-row items-center justify-center space-x-2 rounded-md bg-black text-white" | ||
> | ||
<FontAwesome5 name="discord" size={16} color="white" /> | ||
<Text className="text-lg font-semibold text-white"> | ||
Sign In With Discord | ||
</Text> | ||
</TouchableOpacity> | ||
</View> | ||
|
||
<View className="flex flex-col items-center gap-4 p-2"> | ||
<Text className="mx-auto text-3xl font-bold text-white"> | ||
{session.status === "authenticated" | ||
? `Hello ${session.data.user.name ?? "Unknown"}!` | ||
: "Not Authenticated"} | ||
</Text> | ||
</View> | ||
</View> | ||
</SafeAreaView> | ||
); | ||
}; |
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,128 @@ | ||
import * as AuthSession from "expo-auth-session"; | ||
import { SigninResult, getSignInInfo } from "next-auth/expo"; | ||
|
||
export const nativeProviders = { | ||
discord: "discord-expo", | ||
github: "github-expo", | ||
} as const; | ||
|
||
export const isValidProvider = ( | ||
k: string, | ||
): k is keyof typeof nativeProviders => { | ||
return k in nativeProviders; | ||
}; | ||
|
||
export const nativeDiscoveryUrls = { | ||
discord: { | ||
authorizationEndpoint: "https://discord.com/api/oauth2/authorize", | ||
tokenEndpoint: "https://discord.com/api/oauth2/token", | ||
revocationEndpoint: "https://discord.com/api/oauth2/token/revoke", | ||
}, | ||
github: { | ||
authorizationEndpoint: "https://github.com/login/oauth/authorize", | ||
tokenEndpoint: "https://github.com/login/oauth/access_token", | ||
revocationEndpoint: | ||
"https://github.com/settings/connections/applications/XXXXXXXXXXX", // ignore this, it should be set to a clientId. | ||
}, | ||
}; | ||
|
||
export const socialLogin = async ( | ||
baseProvider: keyof typeof nativeProviders, | ||
): Promise<SigninResult | null> => { | ||
const proxyRedirectUri = AuthSession.makeRedirectUri({ useProxy: true }); | ||
|
||
const provider = isValidProvider(baseProvider) | ||
? nativeProviders[baseProvider] | ||
: baseProvider; | ||
|
||
const signinInfo = await getSignInInfo({ | ||
provider: provider, | ||
proxyRedirectUri, | ||
}); | ||
|
||
console.log("Signin info", signinInfo); | ||
console.log("redirect", proxyRedirectUri); | ||
|
||
if (!signinInfo) { | ||
Alert.alert("Error", "Couldn't get sign in info from server"); | ||
return null; | ||
} | ||
|
||
const { state, codeChallenge, stateEncrypted, codeVerifier, clientId } = | ||
signinInfo; | ||
|
||
const request = new AuthSession.AuthRequest({ | ||
clientId: clientId, | ||
scopes: ["identify", "email"], | ||
redirectUri: proxyRedirectUri, | ||
usePKCE: false, | ||
}); | ||
|
||
const discovery = nativeDiscoveryUrls[baseProvider]; | ||
|
||
request.state = state; | ||
request.codeChallenge = codeChallenge; | ||
await request.makeAuthUrlAsync(discovery); | ||
|
||
const result = await request.promptAsync(discovery, { useProxy: true }); | ||
|
||
return { | ||
result, | ||
state, | ||
stateEncrypted, | ||
codeVerifier, | ||
provider, | ||
}; | ||
}; | ||
|
||
import * as AppleAuthentication from "expo-apple-authentication"; | ||
import Constants from "expo-constants"; | ||
import { Alert } from "react-native"; | ||
|
||
export const nativeApple = async (): Promise<SigninResult | null> => { | ||
const redirectUri = AuthSession.makeRedirectUri({ | ||
useProxy: false, | ||
projectNameForProxy: Constants.manifest2?.extra?.scopeKey as string, | ||
}); | ||
const signinInfo = await getSignInInfo({ | ||
provider: "expo-apple", | ||
proxyRedirectUri: redirectUri, | ||
}); | ||
|
||
if (!signinInfo) { | ||
Alert.alert("Error", "Couldn't get sign in info from server"); | ||
return null; | ||
} | ||
const { state, stateEncrypted, codeVerifier } = signinInfo; | ||
let credential: AppleAuthentication.AppleAuthenticationCredential | undefined; | ||
try { | ||
credential = await AppleAuthentication.signInAsync({ | ||
requestedScopes: [ | ||
AppleAuthentication.AppleAuthenticationScope.EMAIL, | ||
AppleAuthentication.AppleAuthenticationScope.FULL_NAME, | ||
], | ||
state, | ||
}); | ||
} catch (e) { | ||
console.log("Error signing in with Apple", e); | ||
return null; | ||
} | ||
|
||
return { | ||
codeVerifier, | ||
provider: "apple-expo", | ||
result: { | ||
authentication: null, | ||
error: null, | ||
errorCode: null, | ||
params: { | ||
code: credential?.authorizationCode as string, | ||
state, | ||
}, | ||
type: "success", | ||
url: "", | ||
}, | ||
state, | ||
stateEncrypted, | ||
}; | ||
}; |
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,5 +1,11 @@ | ||
import NextAuth from "next-auth"; | ||
|
||
import { authOptions } from "@acme/auth"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
export default NextAuth(authOptions); | ||
// eslint-disable-next-line import/no-anonymous-default-export | ||
export default async (req: NextApiRequest, res: NextApiResponse) => { | ||
// console.log("Incoming request", req); | ||
await NextAuth(req, res, authOptions); | ||
// console.log("Responding with", res.statusCode, res); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be removed