Replies: 2 comments
-
|
Beta Was this translation helpful? Give feedback.
-
That is true, but is it possible to use the internal Lucia functions instead of creating my own? The functionality almost exist, but are not exported as an API. I would really like a generalized version of the Here is a suggestion, change validateCallback: async (code: string) => {
const tokens = await getTokens(code);
const providerUser = await getProvider<GithubUser>("https://api.github.com/user", "bearer", tokens.accessToken);
const providerUserId = providerUser.id.toString();
const providerAuth = await connectAuth(auth, PROVIDER_ID, providerUserId);
return {
...providerAuth,
providerUser,
tokens
};
} Where the previous named export const getProvider = async <T extends {}>(req_url: string, type: 'bearer' | 'basic', accessToken: string) => {
const request = new Request(req_url, {
headers: authorizationHeaders(type, accessToken)
});
return await handleRequest<T>(request);
}; I have a fork with this implemented and it allows me to do the following svelltekit app that is quite neat. // routes/api/oauth/github/+server.ts
type GithubEmail = {
email: string;
verified: boolean;
primary: boolean;
visibility: string;
};
const { existingUser, providerUser, createUser } = await githubAuth.validateCallback(code);
const providerEmails = await getProvider<[GithubEmail]>("https://api.github.com/user/emails", "bearer", tokens.accessToken);
const getUser = async () => {
if (existingUser) return existingUser;
// create a new user if the user does not exist
return await createUser({
// attributes
email: providerEmails.email,
email_verified: false
});
}; I see that many of the providers that you support follow the same structure. A lot of code could be removed by reusing the |
Beta Was this translation helpful? Give feedback.
-
I want to implement GitHub oauth with an email as the primary key, but the
email
field on theproviderUser
object is alwaysnull
. Changing thegithubAuth
scope to include'user:email'
still results in the email field being null.Searching for an answer lead me to the github docs and this stack overflow question that explains that the null email field that is returned to me is for the public email, and since my account has it hidden it will return null. To get the private email another api route has to be fetched from. That other route is
If this is the case what should i do to fetched the private email for github users?
For refrence i am using Sveltekit and this is how i create a new user from the github callback.
The above code causes an error because the
providerUser.email
is null, below is all the fields forproviderUser
Here is how i initialize the githubAuth
Beta Was this translation helpful? Give feedback.
All reactions