-
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.
Merge pull request #69 from HungrySlugs-CSE115A/login
Login
- Loading branch information
Showing
13 changed files
with
262 additions
and
60 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"use client"; | ||
import { useEffect, useState } from "react"; | ||
import { GoogleOAuthProvider, useGoogleLogin, googleLogout, TokenResponse} from "@react-oauth/google"; | ||
import { jwtDecode } from "jwt-decode"; | ||
import axios from "axios"; | ||
|
||
interface User { | ||
name: string; | ||
email: string; | ||
picture: string; | ||
} | ||
const LoginPage = () => { | ||
return( | ||
<GoogleOAuthProvider clientId={'1040494859138-vji3ddfil5jancg23ifaginvmn71hktf.apps.googleusercontent.com'}> | ||
<LoginComponent/> | ||
</GoogleOAuthProvider> | ||
) | ||
} | ||
|
||
const LoginComponent = () => { | ||
const [user, setUser] = useState<User | null>(null); | ||
|
||
|
||
|
||
useEffect(() => { | ||
console.log("LoginPage component mounted"); | ||
}, []); | ||
|
||
const handleLoginSuccess = (tokenResponse: any) => { | ||
if ('code' in tokenResponse) { | ||
// Handle authorization code flow | ||
console.log('Authorization Code:', tokenResponse.code); | ||
// Exchange code for tokens here | ||
} else { | ||
// Handle implicit flow | ||
console.log('Token Received:', tokenResponse.access_token); | ||
const decoded: User = jwtDecode(tokenResponse.id_token); | ||
setUser({ | ||
name: decoded.name, | ||
email: decoded.email, | ||
picture: decoded.picture | ||
}); | ||
// Send token to backend if necessary | ||
axios.post('http://localhost:8000/myapi/users/google-oauth2/', { token: tokenResponse.access_token }) | ||
.then(res => console.log('Backend login successful', res)) | ||
.catch(err => console.error('Backend login failed', err)); | ||
} | ||
|
||
}; | ||
const login = useGoogleLogin({ | ||
flow: "auth-code", | ||
|
||
onSuccess: (tokenResponse) => handleLoginSuccess, | ||
onError: (errorResponse) => console.error('Login Failed', errorResponse), | ||
}); | ||
const handleLogout = () => { | ||
googleLogout(); | ||
setUser(null); // Clears the user state, effectively logging out the user | ||
console.log('Logged out successfully'); | ||
}; | ||
return ( | ||
<div> | ||
<button onClick={() => login()} className="hover:underline decoration-yellow-400 underline-offset-8 m-5 p-2 text-[#003C6C] font-medium text-xl"> | ||
Login with Google | ||
</button> | ||
{user && ( | ||
<div> | ||
<img src={user.picture} alt="User profile" /> | ||
<h2>Welcome, {user.name} - {user.email}</h2> | ||
|
||
</div> | ||
)} | ||
<button onClick={handleLogout} className="hover:underline decoration-yellow-400 underline-offset-8 top-0 right-0 m-5 p-2 text-[#003C6C] font-medium text-xl"> | ||
Logout | ||
</button> | ||
</div> | ||
|
||
); | ||
}; | ||
|
||
export default LoginPage; | ||
|
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,41 @@ | ||
# Generated by Django 5.0.4 on 2024-05-07 04:49 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="UserProfile", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("first_name", models.CharField(blank=True, max_length=50)), | ||
("last_name", models.CharField(blank=True, max_length=50)), | ||
("email", models.EmailField(blank=True, max_length=254)), | ||
( | ||
"user", | ||
models.OneToOneField( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
), | ||
] |
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,18 @@ | ||
# Generated by Django 5.0.4 on 2024-05-07 19:26 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("myapi", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="userprofile", | ||
name="email", | ||
field=models.EmailField(max_length=254, unique=True), | ||
), | ||
] |
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,16 @@ | ||
# Generated by Django 5.0.4 on 2024-05-08 04:25 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("myapi", "0002_alter_userprofile_email"), | ||
] | ||
|
||
operations = [ | ||
migrations.DeleteModel( | ||
name="UserProfile", | ||
), | ||
] |
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,7 +1,10 @@ | ||
from django.urls import path | ||
from django.urls import path, re_path | ||
from . import views | ||
|
||
urlpatterns = [ | ||
path("hello-world/", views.hello_world, name="hello_world"), | ||
path("locations/", views.get_locations, name="locations"), | ||
#path('users/', views.create_user, name='create_user'), | ||
path('users/<str:backend>/', views.create_user, name='create_user'), | ||
#path('getuser/', views.get_user, name='get_user'), | ||
] |
Oops, something went wrong.