aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/login.js
blob: f86ad4465884c16a8e66b677dd324bf7ec6cb4d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React from 'react';
import { Link } from "react-router-dom";

import "./login.css";
import img1 from "../imgs/login.png"
import img2 from "../imgs/10.png"
import img3 from "../imgs/11.png"


export default function Login(prop) {
const {setToken,profile,setProfile} = prop
function login() {
    window.location.href="https://lp.ardapektezol.com/api/v1/login"
}
function logout() {
    setIsLoggedIn(false)
    setProfile(null)
    setToken(null)
    fetch(`https://lp.ardapektezol.com/api/v1/token`,{'method':'DELETE'})
    .then(r=>window.location.href="/")
}
const [isLoggedIn, setIsLoggedIn] = React.useState(false);
React.useEffect(() => {
    fetch(`https://lp.ardapektezol.com/api/v1/token`)
    .then(r => r.json())
    .then(d => {
        if (d.data != null) {
            setToken(d.data.token)
        }
    })
    }, []);


React.useEffect(() => {
    if(profile!==null){setIsLoggedIn(true)}
    }, [profile]);

return (
    <>
    {isLoggedIn ? (
    <Link to="/profile" tabIndex={-1} className='login'>
        <button className='sidebar-button'>
            <img src={profile.avatar_link} alt="" />
            <span>{profile.user_name}</span>
        </button>
        <button className='sidebar-button' onClick={logout}><img src={img3} alt="" /><span></span></button>
    </Link>
    ) : (
    <Link tabIndex={-1} className='login' >
        <button className='sidebar-button' onClick={login}>
            <img src={img2} alt="" />
            <span><img src={img1} alt="Sign in through Steam" /></span>
        </button>
        <button className='sidebar-button' disabled><span></span></button>
    </Link>
    )}
     </>   
        )
}