source: components/Header.jsx

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

Added authentication with google

  • Property mode set to 100644
File size: 5.9 KB
Line 
1import React from 'react'
2
3import Link from 'next/link'
4
5import { useEffect } from 'react'
6import { useDispatch, useSelector } from 'react-redux'
7
8import { setPlayer } from '../redux/reducers/playerSlice'
9import { setStyle } from '../redux/reducers/styleSlice'
10
11import axios from 'axios';
12
13import { signOut, useSession } from 'next-auth/react';
14
15const Header = () => {
16 const { data: googleSession } = useSession();
17
18 useEffect(() => {
19 if (googleSession && (!localStorage.CAESSINO_SESSION_ID || localStorage.CAESSINO_SESSION_ID === "")) {
20 axios.post(`/api/postgre`, {
21 action: 'login_via_google',
22 googleSession: googleSession,
23 })
24 .then(res => {
25 if (res.data?.success) {
26 localStorage.CAESSINO_SESSION_ID = res.data?.session?.id;
27
28 dispatch(setPlayer({
29 ...playerState.player,
30 username: res.data?.session?.username,
31 displayName: res.data?.session?.displayName,
32 credits: res.data?.session.credits,
33 session_id: res.data?.session?.id,
34 }));
35
36 dispatch(setStyle({
37 ...styleState.style,
38 displayLoadingScreen: false,
39 displayLoginScreen: false,
40 loginScreenInfo: {
41 username: '',
42 password: '',
43 },
44 notification: {
45 show: true,
46 text: 'Successfully logged in.',
47 status: 'success',
48 },
49 inlineAlertText: '',
50 }));
51 }
52 });
53 }
54 }, [googleSession])
55
56
57 const dispatch = useDispatch();
58
59 const playerState = useSelector(state => state.player);
60 const styleState = useSelector(state => state.style);
61
62 function register() {
63 dispatch(setStyle({
64 ...styleState.style,
65 displayRegisterScreen: true,
66 registerScreenInfo: {
67 ...styleState.style.registerScreenInfo,
68 setFocus: true
69 }
70 }))
71 }
72
73 function login() {
74 dispatch(setStyle({
75 ...styleState.style,
76 displayLoginScreen: true,
77 loginScreenInfo: {
78 ...styleState.style.loginScreenInfo,
79 setFocus: true
80 }
81 }))
82 }
83
84 function logout() {
85 axios.get(`/api/postgre?action=logout&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
86 if (res.data?.success) {
87 localStorage.removeItem('CAESSINO_SESSION_ID');
88 dispatch(setPlayer({
89 displayName: '',
90 username: '',
91 session_id: '',
92 room_id: '',
93 credits: 0,
94 }))
95 signOut();
96 }
97 })
98 }
99
100 function complain() {
101 dispatch(setStyle({
102 ...styleState.style,
103 displayComplainScreen: true,
104 }))
105 }
106
107 function showStats() {
108 axios.get(`/api/postgre?action=get_stats&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
109 if (res.data?.success) {
110 dispatch(setStyle({
111 ...styleState.style,
112 displayStatsScreen: true,
113 statsScreenInfo: {
114 money: {
115 ...styleState.style.statsScreenInfo.money,
116 earned: res.data.stats.money_earned,
117 },
118 blackjack: {
119 ...styleState.style.statsScreenInfo.blackjack,
120 wins: res.data.stats.blackjack_won_games,
121 },
122 roulette: {
123 ...styleState.style.statsScreenInfo.roulette,
124 wins: res.data.stats.roulette_won_games,
125 },
126 poker: {
127 ...styleState.style.statsScreenInfo.poker,
128 wins: res.data.stats.poker_won_games,
129 }
130 }
131 }))
132 }
133 })
134 }
135
136 function manageCredits() {
137 dispatch(setStyle({
138 ...styleState.style,
139 displayManageCreditsScreen: true,
140 displayDepositModal: false,
141 displayWithdrawModal: false,
142 }))
143 }
144
145 useEffect(() => {
146 if (playerState.player.displayName === '') {
147 dispatch(setStyle({
148 ...styleState.style,
149 displayLoadingScreen: true
150 }));
151
152 let success = false;
153
154 if (localStorage?.CAESSINO_SESSION_ID && localStorage.CAESSINO_SESSION_ID.length > 0) {
155 axios.get(`/api/postgre?action=check_if_logged_in&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
156 if (res.data?.success) {
157 success = true;
158
159 dispatch(setPlayer({
160 ...playerState.player,
161 displayName: res.data?.displayName,
162 username: res.data?.username,
163 session_id: res.data?.session_id,
164 credits: res.data?.credits,
165 }));
166 }
167 });
168 }
169
170 if (!success) {
171 dispatch(setPlayer({
172 ...playerState.player,
173 displayName: 'Guest',
174 }))
175 dispatch(setStyle({
176 ...styleState.style,
177 displayLoadingScreen: false,
178 }))
179 }
180 }
181 }, [dispatch, playerState.player, styleState.style]);
182
183 return (
184 <header className="header">
185 <Link href="/" passHref>
186 <div className="logo">
187
188 </div>
189 </Link>
190 <nav className='mainHeaderNavigation'>
191 <ul>
192 {playerState.player.displayName === '' || playerState.player.displayName === 'Guest' ? (
193 <>
194 <li onClick={() => {register()}}>Register</li>
195 <li onClick={() => {login()}}>Login</li>
196 </>
197 ) : (
198 <>
199 <li onClick={() => {manageCredits()}}>Manage Credits</li>
200 <li onClick={() => {showStats()}}>Statistics</li>
201 <li onClick={() => {complain()}}>Complain</li>
202 <li onClick={() => {logout()}}>Logout</li>
203 </>
204 )}
205 </ul>
206 </nav>
207 </header>
208 )
209}
210
211export default Header
Note: See TracBrowser for help on using the repository browser.