From c1d531a5e2ee7e974e032ffc37a2e8bfeb8649bc Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Thu, 29 Aug 2019 10:14:52 +0100 Subject: [PATCH] feature #12: Add login to User Api --- src/api/authApi copy.js | 12 ++++++++++++ src/api/userApi.js | 15 +++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/api/authApi copy.js diff --git a/src/api/authApi copy.js b/src/api/authApi copy.js new file mode 100644 index 0000000..2c1e7f3 --- /dev/null +++ b/src/api/authApi copy.js @@ -0,0 +1,12 @@ +import { handleResponse, handleError } from "./apiUtils"; +const baseUrl = process.env.REACT_APP_API_URL + "/api/auth/"; + +export function login(userName, password) { + const requestOptions = { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ userName, password }) + }; + + return fetch(requestOptions); +} diff --git a/src/api/userApi.js b/src/api/userApi.js index 0ba7b93..2145062 100755 --- a/src/api/userApi.js +++ b/src/api/userApi.js @@ -13,6 +13,21 @@ export function getOneUser(userId) { .catch(handleError); } +export function login(userName, password) { + const requestOptions = { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ userName, password }) + }; + + return fetch(baseUrl + "/authenticate", requestOptions) + .then(handleResponse) + .then(user => { + localStorage.setItem("trelloid_jwt_token", JSON.stringify(user.token)); + }) + .catch(handleError); +} + export function saveUser(user) { return fetch(baseUrl + (user._id || ""), { method: user._id ? "PUT" : "POST",