fix: github auth login (#250)

* fix: added PROJECT_ISSUES_LIST on the imports (#221)

* fix: github signin by parsing email

* refactor: changed variable names

---------

Co-authored-by: sriram veeraghanta <veeraghanta.sriram@gmail.com>
Co-authored-by: Aaryan Khandelwal <65252264+aaryan610@users.noreply.github.com>
Co-authored-by: Vamsi Kurama <vamsi.kurama@gmail.com>
This commit is contained in:
venplane 2023-02-09 00:01:45 +05:30 committed by GitHub
parent 4ffa31fd02
commit 56030b1c2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 27 deletions

View File

@ -34,7 +34,6 @@ def get_tokens_for_user(user):
def validate_google_token(token, client_id): def validate_google_token(token, client_id):
try: try:
id_info = id_token.verify_oauth2_token( id_info = id_token.verify_oauth2_token(
token, google_auth_request.Request(), client_id token, google_auth_request.Request(), client_id
) )
@ -106,9 +105,19 @@ def get_user_data(access_token: str) -> dict:
resp = requests.get(url=url, headers=headers) resp = requests.get(url=url, headers=headers)
userData = resp.json() user_data = resp.json()
return userData response = requests.get(
url="https://api.github.com/user/emails", headers=headers
).json()
[
user_data.update({"email": item.get("email")})
for item in response
if item.get("primary") is True
]
return user_data
class OauthEndpoint(BaseAPIView): class OauthEndpoint(BaseAPIView):
@ -116,7 +125,6 @@ class OauthEndpoint(BaseAPIView):
def post(self, request): def post(self, request):
try: try:
medium = request.data.get("medium", False) medium = request.data.get("medium", False)
id_token = request.data.get("credential", False) id_token = request.data.get("credential", False)
client_id = request.data.get("clientId", False) client_id = request.data.get("clientId", False)
@ -138,7 +146,6 @@ class OauthEndpoint(BaseAPIView):
email = data.get("email", None) email = data.get("email", None)
if email == None: if email == None:
return Response( return Response(
{ {
"error": "Something went wrong. Please try again later or contact the support team." "error": "Something went wrong. Please try again later or contact the support team."
@ -153,7 +160,6 @@ class OauthEndpoint(BaseAPIView):
mobile_number = uuid.uuid4().hex mobile_number = uuid.uuid4().hex
email_verified = True email_verified = True
else: else:
return Response( return Response(
{ {
"error": "Something went wrong. Please try again later or contact the support team." "error": "Something went wrong. Please try again later or contact the support team."

View File

@ -34,7 +34,7 @@ export const GithubLoginButton: FC<GithubLoginButtonProps> = (props) => {
return ( return (
<Link <Link
href={`https://github.com/login/oauth/authorize?client_id=${NEXT_PUBLIC_GITHUB_ID}&redirect_uri=${loginCallBackURL}`} href={`https://github.com/login/oauth/authorize?client_id=${NEXT_PUBLIC_GITHUB_ID}&redirect_uri=${loginCallBackURL}&scope=read:user,user:email`}
> >
<button className="flex w-full items-center rounded bg-black px-3 py-2 text-sm text-white opacity-90 duration-300 hover:opacity-100"> <button className="flex w-full items-center rounded bg-black px-3 py-2 text-sm text-white opacity-90 duration-300 hover:opacity-100">
<Image <Image

View File

@ -62,27 +62,30 @@ const SignInPage: NextPage = () => {
} }
}; };
const handleGithubSignIn = (githubToken: string) => { const handleGithubSignIn = useCallback(
setLoading(true); (credential: string) => {
authenticationService setLoading(true);
.socialAuth({ authenticationService
medium: "github", .socialAuth({
credential: githubToken, medium: "github",
clientId: NEXT_PUBLIC_GITHUB_ID, credential,
}) clientId: NEXT_PUBLIC_GITHUB_ID,
.then(async () => { })
await onSignInSuccess(); .then(async () => {
}) await onSignInSuccess();
.catch((err) => { })
console.log(err); .catch((err) => {
setToastAlert({ console.log(err);
title: "Error signing in!", setToastAlert({
type: "error", title: "Error signing in!",
message: "Something went wrong. Please try again later or contact the support team.", type: "error",
message: "Something went wrong. Please try again later or contact the support team.",
});
setLoading(false);
}); });
setLoading(false); },
}); [onSignInSuccess, setToastAlert]
}; );
return ( return (
<DefaultLayout <DefaultLayout