File tree Expand file tree Collapse file tree 4 files changed +75
-0
lines changed
Expand file tree Collapse file tree 4 files changed +75
-0
lines changed Original file line number Diff line number Diff line change 1+ // src/components/Login.tsx
2+ import { GoogleLogin } from '@react-oauth/google' ;
3+ import type { CredentialResponse } from '@react-oauth/google' ;
4+ import jwt_decode from 'jwt-decode' ;
5+ import axios from 'axios' ;
6+
7+ const LoginComponent = ( ) => {
8+ const handleLogin = async ( credentialResponse : CredentialResponse ) => {
9+ const token = credentialResponse . credential ;
10+ if ( ! token ) return ;
11+
12+ interface GoogleJwtPayload {
13+ email ?: string ;
14+ name ?: string ;
15+ picture ?: string ;
16+ sub ?: string ;
17+ }
18+
19+ const decoded = jwt_decode < GoogleJwtPayload > ( token ) ;
20+ console . log ( "Zalogowany użytkownik Google:" , decoded ) ;
21+
22+ try {
23+ const res = await axios . post ( "http://localhost:8000/dj-rest-auth/google/" , {
24+ access_token : token ,
25+ } ) ;
26+
27+ const sessionToken = res . data . key ;
28+ sessionStorage . setItem ( "authToken" , sessionToken ) ;
29+ alert ( "Zalogowano!" ) ;
30+ } catch ( error ) {
31+ console . error ( "Błąd podczas logowania przez Google:" , error ) ;
32+ }
33+ } ;
34+
35+ return (
36+ < div >
37+ < h2 > Zaloguj się przez Google</ h2 >
38+ < GoogleLogin
39+ onSuccess = { handleLogin }
40+ onError = { ( ) => console . log ( "Błąd logowania Google" ) }
41+ />
42+ </ div >
43+ ) ;
44+ } ;
45+
46+ export default LoginComponent ;
Original file line number Diff line number Diff line change 1+ import { GoogleOAuthProvider } from '@react-oauth/google' ;
2+ import Login from '../components/LoginComponent' ;
3+
4+ const App = ( ) => {
5+ return (
6+ < GoogleOAuthProvider clientId = "453582868828-5coj2343a3k59i3rqc6frqk2a93oh86g.apps.googleusercontent.com" >
7+ < Login />
8+ </ GoogleOAuthProvider >
9+ ) ;
10+ } ;
11+
12+ export default App ;
Original file line number Diff line number Diff line change 1+ // src/routes/AppRouter.tsx
2+ import { BrowserRouter , Routes , Route } from "react-router-dom" ;
3+ import Home from "../App" ;
4+ import Login from "../pages/Login" ;
5+
6+ const AppRouter = ( ) => {
7+ return (
8+ < BrowserRouter >
9+ < Routes >
10+ < Route path = "/" element = { < Home /> } />
11+ < Route path = "/login" element = { < Login /> } />
12+ </ Routes >
13+ </ BrowserRouter >
14+ ) ;
15+ } ;
16+
17+ export default AppRouter ;
You can’t perform that action at this time.
0 commit comments