source: components/LoginScreen.jsx

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

Added authentication with google

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[87614a5]1import React from 'react'
2
3import { useSelector, useDispatch } from 'react-redux'
4
[64dc53b]5import { useRef } from 'react'
6
[87614a5]7import { setPlayer } from '../redux/reducers/playerSlice';
8import { setStyle } from '../redux/reducers/styleSlice';
9
[22367db]10import { FcGoogle } from 'react-icons/fc';
11import { signIn } from 'next-auth/react';
12
[87614a5]13import axios from 'axios';
14
15const LoginScreen = () => {
[64dc53b]16 const ref = useRef(null);
17
[87614a5]18 const dispatch = useDispatch();
19
20 const playerState = useSelector(state => state.player);
21 const styleState = useSelector(state => state.style);
22
[64dc53b]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
[87614a5]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
[64dc53b]56 function keyUp(e) {
57 if (e.key === 'Enter') {
58 login();
59 }
60 }
61
[87614a5]62 function closeForm() {
63 dispatch(setStyle({
64 ...styleState.style,
65 displayLoginScreen: false,
66 loginScreenInfo: {
67 username: '',
68 password: '',
69 },
70 inlineAlertText: '',
71 }));
72
[64dc53b]73 }
[87614a5]74 function login() {
75 dispatch(setStyle({
76 ...styleState.style,
77 displayLoadingScreen: true,
78 }))
79
80 axios.post(`/api/postgre`, {
81 action: 'login',
[ace7865]82 username: styleState.style.loginScreenInfo.username,
83 password: styleState.style.loginScreenInfo.password,
[87614a5]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 },
[ace7865]110 inlineAlertText: '',
[87614a5]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>
[e007fcd]144 <input ref={ref} type="text" onChange={(e) => {onChangeUsername(e)}} onKeyUp={(e) => keyUp(e)} value={styleState.style.loginScreenInfo.username} placeholder="johndoe"/>
[87614a5]145 <span>Password:</span>
[e007fcd]146 <input type="password" onChange={(e) => {onChangePassword(e)}} onKeyUp={(e) => keyUp(e)} value={styleState.style.loginScreenInfo.password} placeholder="****************"/>
[87614a5]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>
[22367db]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>
[87614a5]152 </div>
153 </div>
154 </div>
155 )
156}
157
158export default LoginScreen
Note: See TracBrowser for help on using the repository browser.