-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from sinatra-pod/ian/fixes
Fix authentication issues.
- Loading branch information
Showing
12 changed files
with
241 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { createUserWithEmailAndPassword, signInWithEmailAndPassword} from "firebase/auth"; | ||
import { auth } from "../../utils/firebase"; | ||
import {useNavigate} from "react-router-dom"; | ||
import {storeUser} from "../../utils/authData"; | ||
|
||
export const useEmailPasswordAuth = (isLogin: boolean = true, email: string, password: string, errCallback) => { | ||
const navigate = useNavigate() | ||
|
||
if(isLogin){ | ||
// login existing | ||
return async (e) => { | ||
e.preventDefault() | ||
await signInWithEmailAndPassword(auth, email, password) | ||
.then((credential) => { | ||
storeUser(credential.user.email) | ||
navigate('/') | ||
}) | ||
.catch((error) => errCallback(error)) | ||
} | ||
}else{ | ||
// register new account | ||
return async (e) => { | ||
e.preventDefault() | ||
await createUserWithEmailAndPassword(auth, email, password) | ||
.then((credential) => { | ||
storeUser(credential.user.email) | ||
navigate('/') | ||
}) | ||
.catch((error) => errCallback(error)) | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {signInWithPopup, GoogleAuthProvider, GithubAuthProvider} from "firebase/auth"; | ||
import { auth } from "../../utils/firebase"; | ||
import { useNavigate } from "react-router-dom"; | ||
import {storeUser} from "../../utils/authData"; | ||
|
||
export enum AuthType { | ||
GOOGLE = 0, | ||
GITHUB = 1, | ||
FACEBOOK = 2 | ||
} | ||
|
||
export const useSocialAuth = (type: AuthType = 0) => { | ||
const navigate = useNavigate() | ||
|
||
// set provider | ||
let provider: GoogleAuthProvider | GithubAuthProvider | null = null | ||
switch (type) { | ||
case AuthType.GOOGLE: | ||
provider = new GoogleAuthProvider() | ||
break | ||
case AuthType.GITHUB: | ||
provider = new GithubAuthProvider() | ||
break | ||
case AuthType.FACEBOOK: | ||
break | ||
} | ||
|
||
return async () => { | ||
if(provider) await signInWithPopup(auth, provider) | ||
.then((credential) => { | ||
storeUser(credential.user.email) | ||
navigate('/') | ||
}) | ||
.catch((_e) => false) | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {AuthData} from "./types"; | ||
import {Constants} from "./constants"; | ||
|
||
export const successAuth = (credential): AuthData => { | ||
return { | ||
status: 'success', | ||
user_data: credential.user | ||
} | ||
} | ||
|
||
export const failureAuth = (error): AuthData => { | ||
return { | ||
status: 'failed', | ||
message: error.message | ||
} | ||
} | ||
|
||
export const storeUser = (email) => localStorage.setItem(Constants.LOGGED_IN_USER, email) | ||
|
||
export const resetUser = () => localStorage.removeItem(Constants.LOGGED_IN_USER) | ||
|
||
export const getUser = (): string|null => localStorage.getItem(Constants.LOGGED_IN_USER) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class Constants{ | ||
public static LOGGED_IN_USER = 'logged-in-user' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
export type Numbers = { | ||
a: number, | ||
b: number | ||
} | ||
|
||
export type AuthData = { | ||
status: string; | ||
message?: string; | ||
user_data?: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.