Commit f190a1b5 authored by Xiyang Lu's avatar Xiyang Lu

[AFP-35] Joe connected frontend and backend/ sent user detail to backend

parent 3bea8ff0
// import {postUser} from '../util/session-api-util'
import {postUser} from '../util/session-api-util'
export const SET_CURRENT_USER = "SET_CURRENT_USER"
export const LOGOUT_USER = "LOGOUT_USER"
export const setCurrentUser = () => {
return {type: SET_CURRENT_USER}
export const setCurrentUser = (user) => {
return {type: SET_CURRENT_USER, user}
}
export const logoutUser = () => {
return {type: LOGOUT_USER}
}
// export const login = (user) => dispatch => {
// return postUser(user).then((response)=>
// dispatch(setCurrentUser(response))
// )
// }
export const login = () => {
return (dispatch) => {
return dispatch(setCurrentUser());
};
};
export const login = (user) => dispatch => {
return postUser(user).then((response)=>(
dispatch(setCurrentUser(response))
)
)
}
// export const login = () => {
// return (dispatch) => {
// return dispatch(setCurrentUser());
// };
// };
export const logOut = () => {
return (dispatch) => {
......
import React, { Component } from 'react'
import Nav from 'react-bootstrap/Nav'
import Navbar from 'react-bootstrap/Navbar'
import NavDropdown from 'react-bootstrap/NavDropdown'
import Session from '../session/session-container'
export default class Header extends Component {
......
......@@ -7,7 +7,7 @@ const mSTP = state => ({
});
const mDTP = dispatch => ({
login: () => dispatch(login()),
login: (userResponse) => dispatch(login(userResponse)),
logOut: () => dispatch(logOut())
});
......
......@@ -6,14 +6,26 @@ const clientId = `${process.env.REACT_APP_GOOGLE_CLIENT_ID}.apps.googleuserconte
export default class Session extends Component {
constructor(props) {
super(props)
this.state = {
logIn: false
}
this.loginSuccess = this.loginSuccess.bind(this)
this.loginFailed = this.loginFailed.bind(this)
this.logOutSuccess = this.logOutSuccess.bind(this)
}
// componentDidMount(){
// this.props.logOut()
// }
loginSuccess = (response) => {
console.log('Login Success: currentUser:', response);
this.props.login()
const {accessToken, tokenId, googleId: userId, profileObj} = response
const {email, familyName: lastName, givenName: firstName} = profileObj
const userResponse = {idToken: tokenId, userId, email, firstName, lastName, accessToken}
this.props.login(userResponse)
this.setState({logIn: true})
}
loginFailed = (response) => {
......@@ -21,14 +33,15 @@ export default class Session extends Component {
}
logOutSuccess = (response) => {
console.log('Logout made successfully');
this.props.logOut()
this.setState({logIn: false})
}
render() {
return (
<div>
{this.props.currentUser === null ?
{console.log(this.props.currentUser)}
{!this.state.logIn ?
<GoogleLogin
clientId={clientId}
buttonText="Login"
......
......@@ -6,7 +6,7 @@ const userReducer = (state = initialState, action) => {
let newState = Object.assign({}, state);
switch (action.type) {
case SET_CURRENT_USER:
return newState.currentUser = { emailAddress: 'fakeEmail@123'};
return newState.currentUser = action.user.data;
case LOGOUT_USER:
return newState.currentUser = null;
......
import axios from 'axios';
export const postUser = (user) => {
return axios.post("http://localhost:8080/api/users", user)
}
// export const postUser = (user) => {
// return currentUser
// return fetch("http://localhost:8080/api/users", {method: "POST", body: JSON.stringify(user), headers: {"Content-Type": "application/json"}})
// }
// export const postLogOutUser = (user) => {
......
Markdown is supported
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