8 |
Spring Boot Firebase Authorization
9 | {user != null ? (
10 |
11 |
12 |

13 |
{user.name}
14 |
15 |
16 |
23 |
24 |
25 | ) : (
26 |
27 |
34 |
35 | )}
36 |
37 | );
38 | };
39 |
40 | export default Navbar;
41 |
--------------------------------------------------------------------------------
/ui-client-side-session-demo/src/components/rolemanager/rolemanager.js:
--------------------------------------------------------------------------------
1 | import { useState } from "react";
2 | import { useAuth } from "contexts/useAuth";
3 | import axios from "axios";
4 | import RolesView from "components/roles/roles";
5 |
6 | const RoleManager = () => {
7 | const { loadingUser, user, isSeller, idToken, refreshToken } = useAuth();
8 | const [loading, setLoading] = useState(false);
9 |
10 | const addRole = (role) => {
11 | setLoading(true);
12 | axios({
13 | url:
14 | process.env.NEXT_PUBLIC_MIDDLEWARE_URL +
15 | "/role/add?uid=" +
16 | user.user_id +
17 | "&role=" +
18 | role,
19 | method: "PUT",
20 | headers: {
21 | Authorization: "Bearer " + idToken,
22 | },
23 | })
24 | .then(() => refreshToken())
25 | .then(() => setLoading(false));
26 | };
27 |
28 | const removeRole = (role) => {
29 | setLoading(true);
30 | axios({
31 | url:
32 | process.env.NEXT_PUBLIC_MIDDLEWARE_URL +
33 | "/role/remove?uid=" +
34 | user.user_id +
35 | "&role=" +
36 | role,
37 | method: "DELETE",
38 | headers: {
39 | Authorization: "Bearer " + idToken,
40 | },
41 | })
42 | .then(() => refreshToken())
43 | .then(() => setLoading(false));
44 | };
45 | return (
46 |
35 | {!loading ? (
36 | <>
37 |
47 | {process.env.NEXT_PUBLIC_TEST_LOGIN == "true" && (
48 |
49 | setTestId(e.target.value)}
53 | />
54 |
66 |
67 | )}
68 | >
69 | ) : (
70 |
{loading}
71 | )}
72 |
73 | );
74 | };
75 |
76 | export default LoginModal;
77 |
--------------------------------------------------------------------------------
/ui-server-side-session-demo/src/components/auth/public-pages.js:
--------------------------------------------------------------------------------
1 | export default ["/_error", "/"];
2 |
--------------------------------------------------------------------------------
/ui-server-side-session-demo/src/config/firebase.config.js:
--------------------------------------------------------------------------------
1 | import { initializeApp } from "firebase/app";
2 |
3 | const config = {
4 | apiKey: process.env.NEXT_PUBLIC_API_KEY,
5 | authDomain: process.env.NEXT_PUBLIC_AUTH_DOMAIN,
6 | databaseURL: process.env.NEXT_PUBLIC_DB_URL,
7 | projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
8 | appId: process.env.NEXT_PUBLIC_APP_ID,
9 | };
10 |
11 | const firebaseApp = initializeApp(config);
12 |
13 | export default firebaseApp;
14 |
--------------------------------------------------------------------------------
/ui-server-side-session-demo/src/contexts/useAuth.js:
--------------------------------------------------------------------------------
1 | import { createContext, useContext, useEffect, useReducer } from "react";
2 | import Cookies from "js-cookie";
3 | import Router from "next/router";
4 | import publicPages from "components/auth/public-pages";
5 | import axios from "axios";
6 |
7 | export const AuthContext = createContext();
8 | const RESET_USER_STATE = "RESET_USER_STATE";
9 |
10 | const initialState = {
11 | isLoading: true,
12 | isAuthenticated: false,
13 | pic: null,
14 | fullname: null,
15 | };
16 |
17 | const userReducer = (state, action) => {
18 | switch (action.type) {
19 | case RESET_USER_STATE:
20 | const appCookies = action.payload;
21 | return {
22 | ...state,
23 | isAuthenticated: appCookies.authenticated,
24 | pic: appCookies.pic,
25 | fullname:
26 | appCookies.fullname && appCookies.fullname.split("_").join(" "),
27 | isLoading: false,
28 | };
29 | default:
30 | return state;
31 | }
32 | };
33 |
34 | export const AuthProvider = (props) => {
35 | const [state, dispatch] = useReducer(userReducer, initialState);
36 |
37 | useEffect(() => {
38 | refreshAuthContext();
39 | }, []);
40 |
41 | useEffect(() => {
42 | if (!state.isLoading) {
43 | const path = props.appProps.router.route;
44 | if (
45 | path != "/" &&
46 | !state.isAuthenticated &&
47 | !publicPages.includes(path)
48 | ) {
49 | Router.push("/");
50 | }
51 | }
52 | }, [state.isAuthenticated, state.isLoading]);
53 |
54 | const refreshAuthContext = () => {
55 | dispatch({ type: RESET_USER_STATE, payload: Cookies.get() });
56 | };
57 |
58 | const logout = () => {
59 | axios({
60 | url: process.env.NEXT_PUBLIC_MIDDLEWARE_URL + "/session/logout",
61 | method: "POST",
62 | headers: {
63 | "Content-Type": "application/json",
64 | },
65 | withCredentials: true,
66 | }).then((res) => {
67 | refreshAuthContext();
68 | });
69 | };
70 |
71 | return (
72 |