Fakultas Ilmu Komputer UI

Commit ea5b43f0 authored by Abraham Rudolf Brahmana's avatar Abraham Rudolf Brahmana Committed by Ahmad Izzudin Alifyandra
Browse files

Redux authentication

parent 26fedfc2
......@@ -2,8 +2,9 @@ import firebase from "firebase/app";
import { Dispatch } from "react-redux/node_modules/@types/react";
import * as userService from "../../service/firestore/user";
import { IUser } from "../../types/firebase/User";
import * as authService from "../../service/firebase/auth";
export const getUser = (uid: string) => {
const getUser = (uid: string) => {
return async (dispatch: Dispatch<any>) => {
try {
const user = await userService.getUser(uid);
......@@ -27,8 +28,65 @@ export const setProfilePic = (url: string, id: string) => {
};
};
//TODO add loginUser
export const loginUser = (email: string, password: string) => {
return async (dispatch: Dispatch<any>) => {
try {
const credential = authService.createCredential(email, password);
const userCredential = await authService.loginUser(credential);
//TODO add logoutUser
const uid = userCredential.user.uid;
dispatch(getUser(uid));
} catch (e) {
console.log(e);
}
};
};
export const logoutUser = () => {
return async (dispatch: Dispatch<any>) => {
try {
await authService.logoutUser();
dispatch({ type: "LOGOUT" });
} catch (e) {
console.log(e);
}
};
};
//TODO change password
export const changePassword = (oldPassword: string, newPassword: string) => {
return async (dispatch: Dispatch<any>) => {
try {
const email = authService.getUserEmail();
const credential = authService.createCredential(email, oldPassword);
await authService.reauthenticateUser(credential);
await authService.changePassword(newPassword);
dispatch({ type: "CHANGE_PASSWORD" });
} catch (e) {
console.log(e);
}
};
};
export const signupUser = (
firstName: string,
lastName: string,
email: string,
password: string
) => {
return async (dispatch: Dispatch<any>) => {
try {
const userAuth = await authService.signUpAuth(email, password);
const uid = userAuth.user.uid;
await userService.createUser(email, firstName, lastName, uid);
dispatch({
type: "SIGNUP",
payload: { email, firstName, lastName, id: uid },
});
} catch (e) {
console.log(e);
}
};
};
......@@ -18,6 +18,10 @@ const userReducer = (state = initialState, action: AnyAction): IUser => {
return initialState;
case "SET_PIC":
return action.payload as IUser;
case "CHANGE_PASSWORD":
return state;
case "SIGNUP":
return action.payload as IUser;
default:
return state;
}
......
import firebase from "firebase/app";
export const createCredential = (email: string, password: string) => {
return firebase.auth.EmailAuthProvider.credential(email, password);
};
export const loginUser = async (credential: firebase.auth.AuthCredential) => {
return await firebase.auth().signInWithCredential(credential);
};
export const logoutUser = async () => {
return await firebase.auth().signOut();
};
export const getUserEmail = (): string => {
return firebase.auth().currentUser.email;
};
export const reauthenticateUser = async (
credential: firebase.auth.AuthCredential
) => {
return await firebase
.auth()
.currentUser.reauthenticateWithCredential(credential);
};
export const changePassword = async (password: string) => {
return await firebase.auth().currentUser.updatePassword(password);
};
export const signUpAuth = async (email: string, password: string) => {
return await firebase.auth().createUserWithEmailAndPassword(email, password);
};
......@@ -15,3 +15,17 @@ export const getUser = async (id) => {
const db = firebase.firestore();
return await db.collection("users").doc(id).get();
};
export const createUser = async (
email: string,
firstName: string,
lastName: string,
uid: string
) => {
const db = firebase.firestore();
return await db.collection("users").doc(uid).set({
firstName,
lastName,
email,
});
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment