Skip to content

Commit

Permalink
Updated FE OAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
Alder Whiteford authored and Alder Whiteford committed Jun 20, 2024
1 parent 5bf3021 commit 4652be8
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 41 deletions.
1 change: 1 addition & 0 deletions backend/integrations/oauth/soth/msft_web/msft_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var defaultScopes = []string{"openid", "offline_access", "user.read", "calendars
func New(clientKey *m.Secret[string], secret *m.Secret[string], callbackURL string, tenant string, scopes ...string) *Provider {
p := &Provider{
ClientKey: clientKey,
Secret: secret,

Check failure on line 37 in backend/integrations/oauth/soth/msft_web/msft_web.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
CallbackURL: callbackURL,
ProviderName: "microsoftonlineweb",
tenant: tenant,
Expand Down
2 changes: 1 addition & 1 deletion frontend/lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@generatesac/lib",
"version": "0.0.180",
"version": "0.0.184",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion frontend/lib/src/api/authApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LoginResponse, OAuthCallbackRequestQueryParams } from "../types/auth";
import { baseApi, handleQueryParams } from "./base";

const AUTH_API_BASE_URL = "/auth";
const PROVIDER = "microsoftonline";
const PROVIDER = "microsoftonlineweb";

export const authApi = baseApi.injectEndpoints({
endpoints: (builder) => ({
Expand Down
19 changes: 14 additions & 5 deletions frontend/lib/src/api/base.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const LOCAL_API_BASE_URL = "http://localhost:3000/api/v1";
export const LOCAL_API_BASE_URL = "http://127.0.0.1:8080/api/v1";
export const PROD_API_BASE_URL = "https://studentactivitycalendar.xyz/api/v1";

type GlobalState = {
platform: "web" | "mobile";
loggedIn: boolean;
accessToken?: string;
};

// BaseAPI for the entire application:
export const baseApi = createApi({
baseQuery: fetchBaseQuery({
baseUrl: PROD_API_BASE_URL,
baseUrl: LOCAL_API_BASE_URL,
credentials: "include",
prepareHeaders: async (headers, { getState }) => {
// User slice existing must exist in all dependent apps:
const token = (getState() as { user: { accessToken: string } })?.user?.accessToken;
if (token) {
headers.set("_sac_session", token);
const globalState = (getState() as { global: GlobalState })?.global;
if (globalState.accessToken) {
// Pass the authentication header:
headers.set("_sac_session", globalState.accessToken);
}
// Set the platform header:
headers.set("Platform", globalState.platform);
return headers;
},
}),
Expand Down
2 changes: 1 addition & 1 deletion frontend/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@fortawesome/free-solid-svg-icons": "^6.5.2",
"@fortawesome/react-fontawesome": "^0.2.2",
"@fortawesome/react-native-fontawesome": "^0.3.2",
"@generatesac/lib": "0.0.180",
"@generatesac/lib": "0.0.184",
"@gorhom/bottom-sheet": "^4.6.3",
"@hookform/resolvers": "^3.4.2",
"@react-native-async-storage/async-storage": "^1.23.1",
Expand Down
2 changes: 1 addition & 1 deletion frontend/mobile/src/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export { ErrorBoundary } from 'expo-router';
SplashScreen.preventAutoHideAsync();

const InitalLayout = () => {
const { accessToken, loggedIn } = useAppSelector((state) => state.user);
const { accessToken, loggedIn } = useAppSelector((state) => state.global);
const {
eventPreviewRef,
eventId,
Expand Down
2 changes: 1 addition & 1 deletion frontend/mobile/src/app/app/(tabs)/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';

import { GlobalLayout } from '@/src/app/design-system/components/GlobalLayout/GlobalLayout';
import { resetAccessToken } from '@/src/store/slices/userSlice';
import { useAppDispatch, useAppSelector } from '@/src/store/store';

Check failure on line 16 in frontend/mobile/src/app/app/(tabs)/profile.tsx

View workflow job for this annotation

GitHub Actions / Lint

Insert `resetAccessToken·}·from·'@/src/store/slices/globalSlice';⏎import·{·`

import { Box, Colors, SACColors, Spacing, Text } from '../../design-system';

Check failure on line 18 in frontend/mobile/src/app/app/(tabs)/profile.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `';⏎import·{·resetAccessToken·}·from·'@/src/store/slices/globalSlice`
import { resetAccessToken } from '@/src/store/slices/globalSlice';

type ProfileItemProps = {
icon: IconDefinition;
Expand Down
7 changes: 4 additions & 3 deletions frontend/mobile/src/app/auth/callback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import { router, useLocalSearchParams } from 'expo-router';
import { OAuthCallbackRequestQueryParams, authApi } from '@generatesac/lib';

import Loading from '@/src/assets/gif/loading.gif';
import { setLoggedInState, setUser } from '@/src/store/slices/userSlice';
import { setUser } from '@/src/store/slices/userSlice';

Check failure on line 9 in frontend/mobile/src/app/auth/callback.tsx

View workflow job for this annotation

GitHub Actions / Lint

Insert `LoggedIn·}·from·'@/src/store/slices/globalSlice';⏎import·{·set`
import { useAppDispatch, useAppSelector } from '@/src/store/store';

import { Box, Text } from '../design-system';

Check failure on line 12 in frontend/mobile/src/app/auth/callback.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `';⏎import·{·setLoggedIn·}·from·'@/src/store/slices/globalSlice`
import { setLoggedIn } from '@/src/store/slices/globalSlice';

const OAuthCallback = () => {
const params = useLocalSearchParams();
const [oAuthCallback] = authApi.useLazyCallbackQuery();
const dispatch = useAppDispatch();
const { accessToken } = useAppSelector((state) => state.user);
const { accessToken } = useAppSelector((state) => state.global);

useEffect(() => {
const { code, session_state, state } =
Expand All @@ -25,7 +26,7 @@ const OAuthCallback = () => {
({ data, error }) => {
if (data) {
dispatch(setUser(data));
dispatch(setLoggedInState(true));
dispatch(setLoggedIn(true));
router.push('/app/');
}
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/mobile/src/app/auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { Image, Linking, SafeAreaView } from 'react-native';
import { authApi } from '@generatesac/lib';

import Loading from '@/src/assets/gif/loading.gif';
import { setAccessToken } from '@/src/store/slices/userSlice';
import { useAppDispatch } from '@/src/store/store';

Check failure on line 7 in frontend/mobile/src/app/auth/index.tsx

View workflow job for this annotation

GitHub Actions / Lint

Insert `setAccessToken·}·from·'@/src/store/slices/globalSlice';⏎import·{·`

import { Box, Button, Text } from '../design-system';
import { GlobalLayout } from '../design-system/components/GlobalLayout/GlobalLayout';

Check failure on line 10 in frontend/mobile/src/app/auth/index.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `';⏎import·{·setAccessToken·}·from·'@/src/store/slices/globalSlice`
import { setAccessToken } from '@/src/store/slices/globalSlice';

const WelcomePage = () => {
const [login, { isLoading }] = authApi.useLazyLoginQuery();
Expand Down
2 changes: 0 additions & 2 deletions frontend/mobile/src/hooks/useClub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const useClub = () => {
if (clubId !== '') {
getClub(clubId).then(({ data: clubData }) => {
if (clubData) {
console.log(clubData);
dispatch(setClub(clubData));
}
});
Expand All @@ -50,7 +49,6 @@ const useClub = () => {
new Date(event.end_time).getTime() >
new Date().getTime()
);
console.log('events', events);
dispatch(setClubEvents(events));
}
});
Expand Down
37 changes: 37 additions & 0 deletions frontend/mobile/src/store/slices/globalSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Event, Tag } from '@generatesac/lib';

Check failure on line 1 in frontend/mobile/src/store/slices/globalSlice.ts

View workflow job for this annotation

GitHub Actions / Lint

'Event' is defined but never used

Check failure on line 1 in frontend/mobile/src/store/slices/globalSlice.ts

View workflow job for this annotation

GitHub Actions / Lint

'Tag' is defined but never used
import { createSlice } from '@reduxjs/toolkit';

type GlobalState = {
platform: 'web' | 'mobile';
loggedIn: boolean;
accessToken?: string;
};

const initialState: GlobalState = {
platform: 'mobile',
loggedIn: false,

Check failure on line 12 in frontend/mobile/src/store/slices/globalSlice.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `,`
};

export const globalSlice = createSlice({
name: 'global',
initialState,
reducers: {
setLoggedIn: (state, action) => {
state.loggedIn = action.payload;
},
setAccessToken: (state, action) => {
state.accessToken = action.payload;
},
resetAccessToken: (state) => {
state.accessToken = undefined;
state.loggedIn = false;
}
}
});

export const {

Check failure on line 32 in frontend/mobile/src/store/slices/globalSlice.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎····setLoggedIn,⏎····setAccessToken,⏎····resetAccessToken⏎}·=` with `·setLoggedIn,·setAccessToken,·resetAccessToken·}·=⏎···`
setLoggedIn,
setAccessToken,
resetAccessToken
} = globalSlice.actions;
export default globalSlice.reducer;
21 changes: 1 addition & 20 deletions frontend/mobile/src/store/slices/userSlice.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { User } from '@generatesac/lib';
import { createSlice } from '@reduxjs/toolkit';

export type UserState = {
loggedIn: boolean;
accessToken?: string;
};

const initialState: UserState & User = {
const initialState: User = {
id: '',
email: '',
loggedIn: false,
created_at: '',
updated_at: '',
name: '',
Expand All @@ -33,24 +27,11 @@ export const userSlice = createSlice({
state.role = action.payload.role;
},
resetUser: () => initialState,
setAccessToken: (state, action) => {
state.accessToken = action.payload;
},
resetAccessToken: (state) => {
state.accessToken = undefined;
state.loggedIn = false;
},
setLoggedInState: (state, action) => {
state.loggedIn = action.payload;
}
}
});

export const {
setUser,
resetUser,
setAccessToken,
resetAccessToken,
setLoggedInState
} = userSlice.actions;
export default userSlice.reducer;
4 changes: 3 additions & 1 deletion frontend/mobile/src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import { thunk } from 'redux-thunk';
import clubReducer from '@/src/store/slices/clubSlice';
import eventReducer from '@/src/store/slices/eventSlice';
import userReducer from '@/src/store/slices/userSlice';
import globalReducer from './slices/globalSlice';

const rootReducer = combineReducers({
global: globalReducer,
user: userReducer,
event: eventReducer,
club: clubReducer,
Expand All @@ -34,7 +36,7 @@ const persistanceConfig = {
storage: AsyncStorage,
stateReconciler: autoMergeLevel2,
blacklist: [baseApi.reducerPath],
whitelist: ['user', 'event', 'club']
whitelist: ['user', 'event', 'club', 'global']
};

const persistedReducer = persistReducer<RootReducer>(
Expand Down
8 changes: 4 additions & 4 deletions frontend/mobile/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1402,10 +1402,10 @@
humps "^2.0.1"
prop-types "^15.7.2"

"@generatesac/[email protected].180":
version "0.0.180"
resolved "https://registry.yarnpkg.com/@generatesac/lib/-/lib-0.0.180.tgz#01a5866af05ccbd369e14faddb8acb2745b9fece"
integrity sha512-UxIBGebRwwHdNyX8CHMmMTbC6WwQMKbvzAtRGCZyMvkldasw+XdQ9+F+WzZyR6OF1Lp84VLJsF6UAIYLO1slVQ==
"@generatesac/[email protected].184":
version "0.0.184"
resolved "https://registry.yarnpkg.com/@generatesac/lib/-/lib-0.0.184.tgz#1a024934e05e69685a46bfcfe388fdf8b4809226"
integrity sha512-btRNxaCH7ovI9WCRt/0fsaUTW2mWzhgDZwBC6fDmIH929bo6etX477jPAdtBJXAWxMz/RIYvIAuLRda8qma/oA==
dependencies:
"@reduxjs/toolkit" "^2.2.3"
react "^18.2.0"
Expand Down

0 comments on commit 4652be8

Please sign in to comment.