source: components/LoginScreen.jsx@ 22367db

main
Last change on this file since 22367db was 22367db, checked in by anastasovv <simon@…>, 2 years ago

Added authentication with google

  • Property mode set to 100644
File size: 5.5 KB
Line 
1import React from 'react'
2
3import { useSelector, useDispatch } from 'react-redux'
4
5import { useRef } from 'react'
6
7import { setPlayer } from '../redux/reducers/playerSlice';
8import { setStyle } from '../redux/reducers/styleSlice';
9
10import { FcGoogle } from 'react-icons/fc';
11import { signIn } from 'next-auth/react';
12
13import axios from 'axios';
14
15const LoginScreen = () => {
16 const ref = useRef(null);
17
18 const dispatch = useDispatch();
19
20 const playerState = useSelector(state => state.player);
21 const styleState = useSelector(state => state.style);
22
23 setTimeout(() => {
24 if (styleState.style.displayLoginScreen && styleState.style.loginScreenInfo.setFocus) {
25 ref.current.focus();
26 dispatch(setStyle({
27 ...styleState.style,
28 loginScreenInfo: {
29 ...styleState.style.loginScreenInfo,
30 setFocus: false
31 }
32 }))
33 }
34 }, 10);
35
36 function onChangeUsername(e) {
37 dispatch(setStyle({
38 ...styleState.style,
39 loginScreenInfo: {
40 ...styleState.style.loginScreenInfo,
41 username: e.target.value,
42 }
43 }))
44 }
45
46 function onChangePassword(e) {
47 dispatch(setStyle({
48 ...styleState.style,
49 loginScreenInfo: {
50 ...styleState.style.loginScreenInfo,
51 password: e.target.value,
52 }
53 }))
54 }
55
56 function keyUp(e) {
57 if (e.key === 'Enter') {
58 login();
59 }
60 }
61
62 function closeForm() {
63 dispatch(setStyle({
64 ...styleState.style,
65 displayLoginScreen: false,
66 loginScreenInfo: {
67 username: '',
68 password: '',
69 },
70 inlineAlertText: '',
71 }));
72
73 }
74 function login() {
75 dispatch(setStyle({
76 ...styleState.style,
77 displayLoadingScreen: true,
78 }))
79
80 axios.post(`/api/postgre`, {
81 action: 'login',
82 username: styleState.style.loginScreenInfo.username,
83 password: styleState.style.loginScreenInfo.password,
84 })
85 .then(res => {
86 if (res.data?.success) {
87 localStorage.CAESSINO_SESSION_ID = res.data?.session?.id;
88
89 dispatch(setPlayer({
90 ...playerState.player,
91 username: res.data?.session?.username,
92 displayName: res.data?.session?.displayName,
93 credits: res.data?.session.credits,
94 session_id: res.data?.session?.id,
95 }));
96
97 dispatch(setStyle({
98 ...styleState.style,
99 displayLoadingScreen: false,
100 displayLoginScreen: false,
101 loginScreenInfo: {
102 username: '',
103 password: '',
104 },
105 notification: {
106 show: true,
107 text: 'Successfully logged in.',
108 status: 'success',
109 },
110 inlineAlertText: '',
111 }));
112 }
113 else {
114 dispatch(setStyle({
115 ...styleState.style,
116 displayLoginScreen: true,
117 inlineAlertText: res.data?.message,
118 }));
119 }
120 })
121 }
122
123 function checkForLink(e) {
124 if (e.target.innerHTML.includes('Register')) {
125 dispatch(setStyle({
126 ...styleState.style,
127 displayRegisterScreen: true,
128 displayLoginScreen: false,
129 loginScreenInfo: {
130 username: '',
131 password: '',
132 },
133 inlineAlertText: '',
134 }));
135 }
136 }
137
138 return (
139 <div className="fullscreen fs-centered loginScreen" style={{display: styleState.style.displayLoginScreen ? 'block' : 'none'}}>
140 <div className="fs-inputs-container">
141 {styleState.style.inlineAlertText.length > 0 && <span className="inlineAlert" onClick={(e) => checkForLink(e)}>{styleState.style.inlineAlertText}</span>}
142 <div>
143 <span>Username:</span>
144 <input ref={ref} type="text" onChange={(e) => {onChangeUsername(e)}} onKeyUp={(e) => keyUp(e)} value={styleState.style.loginScreenInfo.username} placeholder="johndoe"/>
145 <span>Password:</span>
146 <input type="password" onChange={(e) => {onChangePassword(e)}} onKeyUp={(e) => keyUp(e)} value={styleState.style.loginScreenInfo.password} placeholder="****************"/>
147 <div style={{width: '100%', display: 'flex', flexDirection: 'row', justifyContent: 'space-between'}}>
148 <button className="primaryButton" onClick={() => closeForm()}>Close Form</button>
149 <button className="secondaryButton" onClick={() => login()}>Log In</button>
150 </div>
151 <button onClick={() => signIn()} className="primaryButton" style={{marginTop: '10rem', color: '#eee', padding: '.6rem 2rem'}}><FcGoogle style={{padding: '.2rem', background: '#eee', borderRadius: '50%', fontSize: '2rem', marginBottom: '-.4rem'}}/> Log In With Google</button>
152 </div>
153 </div>
154 </div>
155 )
156}
157
158export default LoginScreen
Note: See TracBrowser for help on using the repository browser.