Ignore:
Timestamp:
01/21/25 03:08:24 (2 weeks ago)
Author:
stefan toskovski <stefantoska84@…>
Branches:
main
Parents:
0c6b92a
Message:

F4 Finalna Verzija

File:
1 edited

Legend:

Unmodified
Added
Removed
  • imaps-frontend/src/pages/Login/Login.jsx

    r0c6b92a r79a0317  
    66import HttpService from "../../scripts/net/HttpService.js";
    77import {useAppContext} from "../../components/AppContext/AppContext.jsx";
    8 import config from "../../scripts/net/netconfig.js";
     8import config, {API_BASE_URL} from "../../scripts/net/netconfig.js";
     9import google_icon from "../../assets/Logo-google-icon-PNG.png"
     10import github_icon from "../../assets/github-mark-white.png";
     11import { v4 as uuidv4 } from 'uuid';
    912
    1013const LoginPage = () => {
     
    2629    const handleLogin = async () => {
    2730        const httpService = new HttpService();
    28         return httpService.post(config.auth.login, payload)
    29 
     31        return httpService.post(config.auth.login, payload);
    3032    };
    31 
    3233
    3334    const login = async (e) => {
     
    3738            .then(resp => {
    3839                if (resp.token) {
    39                     navigate(targetPath)
     40                    navigate(targetPath);
    4041                    localStorage.setItem("token", resp.token);
    4142                    setUsername(resp.username);
    4243                    setIsAuthenticated(true);
    43                     console.log("ROLES",resp.roles)
     44                    console.log("ROLES", resp.roles);
    4445                } else {
    4546                    setError("Invalid username or password.");
     
    4748            }).catch(reason => {
    4849            console.error("Login failed", reason);
    49             setError("Login failed. Please try again.")
    50         })
     50            setError("Login failed. Please try again.");
     51        });
     52    };
    5153
    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
    7777    };
    7878
     
    111111                    <button type="submit">Submit</button>
    112112                </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>
    113124                <p>
    114125                    Don't have an account? <Link to="/Signup"> Sign Up </Link>
Note: See TracChangeset for help on using the changeset viewer.