Changeset 79a0317 for imaps-frontend/src/pages/Login/Login.jsx
- Timestamp:
- 01/21/25 03:08:24 (2 weeks ago)
- Branches:
- main
- Parents:
- 0c6b92a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
imaps-frontend/src/pages/Login/Login.jsx
r0c6b92a r79a0317 6 6 import HttpService from "../../scripts/net/HttpService.js"; 7 7 import {useAppContext} from "../../components/AppContext/AppContext.jsx"; 8 import config from "../../scripts/net/netconfig.js"; 8 import config, {API_BASE_URL} from "../../scripts/net/netconfig.js"; 9 import google_icon from "../../assets/Logo-google-icon-PNG.png" 10 import github_icon from "../../assets/github-mark-white.png"; 11 import { v4 as uuidv4 } from 'uuid'; 9 12 10 13 const LoginPage = () => { … … 26 29 const handleLogin = async () => { 27 30 const httpService = new HttpService(); 28 return httpService.post(config.auth.login, payload) 29 31 return httpService.post(config.auth.login, payload); 30 32 }; 31 32 33 33 34 const login = async (e) => { … … 37 38 .then(resp => { 38 39 if (resp.token) { 39 navigate(targetPath) 40 navigate(targetPath); 40 41 localStorage.setItem("token", resp.token); 41 42 setUsername(resp.username); 42 43 setIsAuthenticated(true); 43 console.log("ROLES", resp.roles)44 console.log("ROLES", resp.roles); 44 45 } else { 45 46 setError("Invalid username or password."); … … 47 48 }).catch(reason => { 48 49 console.error("Login failed", reason); 49 setError("Login failed. Please try again.") 50 }) 50 setError("Login failed. Please try again."); 51 }); 52 }; 51 53 52 // fetch("http://localhost:8080/api/auth/login", { 53 // method: "POST", 54 // headers: { 55 // "Content-Type": "application/json", 56 // }, 57 // body: JSON.stringify(payload), 58 // }) 59 // .then((response) => { 60 // if (!response.ok) { 61 // throw new Error("Login failed: resp = " + response.statusText); 62 // } 63 // return response.json(); 64 // }) 65 // .then((data) => { 66 // if (data.token) { 67 // navigate(targetPath); 68 // handleLogin(data); 69 // } else { 70 // setError("Invalid username or password."); 71 // } 72 // }) 73 // .catch((error) => { 74 // console.error("Login failed", error); 75 // setError("Login failed. Please try again."); 76 // }); 54 const continueWithGitHub = async () => { 55 const httpService = new HttpService(); 56 httpService.setResponseType('text'); 57 const state = await httpService.get(config.auth.oauth.github.state) 58 const clientId = 'Iv23liqzhX5wMYNDHtnz'; 59 const redirectUri = encodeURI(`${API_BASE_URL}/oauth/callback/github`); 60 61 const githubAuthUrl = `https://github.com/login/oauth/authorize?client_id=${encodeURI(clientId)}&redirect_uri=${redirectUri}&state=${encodeURI(state)}&scope=user:email`; 62 63 window.location.href = githubAuthUrl; 64 65 }; 66 67 const continueWithGoogle = async () => { 68 console.log("Continue with Google"); 69 const httpService = new HttpService(); 70 httpService.setResponseType('text'); 71 const state = await httpService.get(config.auth.oauth.github.state) 72 const clientId = '1024418489231-ml40ukvqcg9ad1h5ejor5dm6ipt6p8fo.apps.googleusercontent.com'; 73 const redirectUri = encodeURI(`${API_BASE_URL}/oauth/callback/google`); 74 const googleAuthUrl = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${encodeURIComponent(clientId)} 75 &redirect_uri=${encodeURIComponent(redirectUri)}&state=${encodeURIComponent(state)}&response_type=code&scope=${encodeURIComponent("openid profile email")}`; 76 window.location.href = googleAuthUrl 77 77 }; 78 78 … … 111 111 <button type="submit">Submit</button> 112 112 </form> 113 <div className={styles.or}>OR</div> 114 <div className={styles.socialButtons}> 115 <button className={styles.socialButton} onClick={continueWithGoogle}> 116 <img src={google_icon} alt="Facebook Icon" className={styles.socialIcon}/> 117 Sign In With Google 118 </button> 119 <button className={styles.socialButton} onClick={continueWithGitHub}> 120 <img src={github_icon} alt="GitHub Icon" className={styles.socialIcon}/> 121 Sign In With GitHub 122 </button> 123 </div> 113 124 <p> 114 125 Don't have an account? <Link to="/Signup"> Sign Up </Link>
Note:
See TracChangeset
for help on using the changeset viewer.