Commit c66ccc1a authored by Darrick Yong's avatar Darrick Yong

add session connection to frontend

parent 3c12209b
import * as SessionAPIUtil from "../util/session_api_util"; import * as SessionAPIUtil from "../util/session_api_util";
export const LOGIN_USER = 'LOGIN_USER'; export const LOGIN_USER = "LOGIN_USER";
export const LOGOUT_USER = 'LOGOUT_USER'; export const LOGOUT_USER = "LOGOUT_USER";
const receiveLogin = (user) => ({ const receiveLogin = (user) => ({
type: LOGIN_USER, type: LOGIN_USER,
user, user,
}) });
const receiveLogout = () => ({ const receiveLogout = () => ({
type: LOGOUT_USER, type: LOGOUT_USER,
}) });
export const login = payload => dispatch => { export const login = (payload) => (dispatch) => {
SessionAPIUtil.createSession(payload) SessionAPIUtil.createSession(payload).then((res) => {
.then(res => { const { status, data } = res;
dispatch(receiveLogin(res)) if (status < 299) {
}) dispatch(receiveLogin(data));
} }
});
};
export const logout = () => dispatch => { export const logout = () => (dispatch) => {
dispatch(receiveLogout()); dispatch(receiveLogout());
} };
// import axios from 'axios'; import axios from "axios";
export const createSession = (payload) => { export const createSession = (payload) => {
const { token, user } = payload; return axios
.post("http://localhost:8080/api/auth", payload)
// call axios with token .catch((err) => err.response);
return new Promise((resolve, reject) => {
resolve(user);
})
}; };
...@@ -7,8 +7,8 @@ import org.springframework.http.ResponseEntity; ...@@ -7,8 +7,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@RestController @RestController
@RequestMapping(value = "") @RequestMapping(value = "/api")
public class AuthenticationController { public class SessionController {
@Autowired @Autowired
SessionService sessionService; SessionService sessionService;
......
...@@ -7,6 +7,7 @@ import org.springframework.stereotype.Service; ...@@ -7,6 +7,7 @@ import org.springframework.stereotype.Service;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.Scanner;
@Service @Service
public class SessionService { public class SessionService {
...@@ -29,8 +30,8 @@ public class SessionService { ...@@ -29,8 +30,8 @@ public class SessionService {
.body(session); .body(session);
} }
return ResponseEntity return ResponseEntity
.status(HttpStatus.BAD_REQUEST) .status(HttpStatus.UNAUTHORIZED)
.body(null); .body(session);
} }
......
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