Skip to content

Commit

Permalink
feat: GitHub OAuth redirect flow improved #145
Browse files Browse the repository at this point in the history
  • Loading branch information
Nishchit14 committed Nov 6, 2023
1 parent c3dd441 commit 4021d43
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { EProvider } from '../../../services/auth/types';
import { _misc } from '@firecamp/utils';
import { EFirecampAgent } from '@firecamp/types';
import platformContext from '../../../services/platform-context';
import AppService from '../../../services/app.service';

const GithubGoogleAuth: FC<IGithubGoogleAuth> = ({ onClose }) => {
const [disableSignInWithGoogleButton, setDisableSignInWithGoogleButton] =
Expand Down Expand Up @@ -37,9 +38,13 @@ const GithubGoogleAuth: FC<IGithubGoogleAuth> = ({ onClose }) => {
return _auth
.signIn(EProvider.GITHUB)
.then(async ({ response, provider }) => {
// note: this'll be reachable only for desktop environment
await platformContext.app.initApp();
platformContext.app.modals.close();
// note: this'll be reachable only for desktop environment
await platformContext.app.initApp().then(() => {
AppService.notify.success(`You're signed in successfully.`, {
labels: { alert: 'success' },
});
});
await closeModal();
})
.catch((e) => {
Expand Down
48 changes: 36 additions & 12 deletions platform/firecamp-platform/src/services/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { useEnvStore } from '../store/environment';
import { useModalStore } from '../store/modal';
import { platformEmitter } from './platform-emitter';
import { useExplorerStore } from '../store/explorer';
import _auth from '../services/auth';
import { EProvider } from '../services/auth/types';

const userService = {
isLoggedIn: () => {
Expand Down Expand Up @@ -47,14 +49,44 @@ const switchWorkspace = async (

//initialize app flow on first load, after login and after signup
const initApp = async () => {
const { fetchExplorer } = useExplorerStore.getState();
CloudApiGlobal.setHost(process.env.FIRECAMP_API_HOST);
const urlParams = new URLSearchParams(location.search);
console.log(urlParams);
const code = urlParams.get('code');
const _error = urlParams.get('error');
const errorDescription = urlParams.get('error_description');

// Set client id and app version into cloud-api headers
CloudApiGlobal.setHost(process.env.FIRECAMP_API_HOST);
// set app version into cloud-api headers
CloudApiGlobal.setGlobalHeaders({
[ECloudApiHeaders.AppVersion]: process.env.APP_VERSION || '',
});

Promise.resolve()
.then(async () => {
if (code) {
await _auth
.signIn(EProvider.GITHUB, { username: '', password: '' }, code)
.then(async () => {
AppService.notify.success(`You're signed in successfully.`, {
labels: { alert: 'success' },
});
//@ts-ignore
window?.history?.replaceState({}, '', '/');
})
.catch((e) => {
// setError(e.response?.data?.message || e.message);
});
}
return;
})
.finally(() => {
initSession();
});
// if (errorDescription) setError(errorDescription);
};

const initSession = async () => {
const { fetchExplorer } = useExplorerStore.getState();
const socketId = localStorage.getItem('socketId');

//1/ check if user is logged in or not
Expand All @@ -73,15 +105,6 @@ const initApp = async () => {
await fetchExplorer(workspace.__ref.id);
return res.data;
})
.then((res) => {
// if auth happens via github/google then success message would be set at localStorage from identity page
const sMessage = localStorage.getItem('authSuccessMessage');
if (sMessage) {
AppService.notify.success(sMessage);
localStorage.removeItem('authSuccessMessage');
}
return res;
})
.then(({ user }) => {
// subscribe request changes (pull actions)
try {
Expand Down Expand Up @@ -119,6 +142,7 @@ const initApp = async () => {
})
.catch(console.log);
};

const initUser = (user: any) => {
const { setUser } = useUserStore.getState();
setUser(user);
Expand Down
4 changes: 2 additions & 2 deletions platform/firecamp-platform/src/services/auth/oauth2/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EFirecampAgent } from '@firecamp/types';
import { _misc, _string } from '@firecamp/utils';
import { GITHUB_CONFIG } from './constants';

const { CLIENT_ID, SCOPE, AUTH_URL } = GITHUB_CONFIG;
const { CLIENT_ID, SCOPE, AUTH_URL, REDIRECT_URL } = GITHUB_CONFIG;

export const authorize = {
electron: (): Promise<string> => {
Expand All @@ -12,7 +12,7 @@ export const authorize = {
web: (): void => {
// web flow
const scope = SCOPE.join(',');
const redirectUrl = `${location.origin}/identity.html?redirect=${location.href}`;
const redirectUrl = `${REDIRECT_URL}/?redirect=${location.href}`;
const url = `${AUTH_URL}?client_id=${CLIENT_ID}&redirect_uri=${redirectUrl}&scope=${scope}`;
// console.log(url);
// @ts-ignore
Expand Down

0 comments on commit 4021d43

Please sign in to comment.