From 002bad0a919e6708a19dad727a018abcdb287d3a Mon Sep 17 00:00:00 2001 From: jwu-fisker Date: Wed, 6 Jan 2021 09:23:19 -0800 Subject: [PATCH] Move api calls into services --- src/components/contexts/UserContext.jsx | 39 +++---------------------- src/services/auth.js | 33 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 35 deletions(-) create mode 100644 src/services/auth.js diff --git a/src/components/contexts/UserContext.jsx b/src/components/contexts/UserContext.jsx index 2a12d7d..9ea4df3 100644 --- a/src/components/contexts/UserContext.jsx +++ b/src/components/contexts/UserContext.jsx @@ -1,6 +1,6 @@ import React, { useContext, useEffect, useState } from 'react'; +import auth from '../../services/auth'; -const AUTH_URL = 'https://dev-auth.fiskerdps.com'; const UserContext = React.createContext(); export const UserProvider = ({ children }) => { @@ -13,14 +13,7 @@ export const UserProvider = ({ children }) => { const token = JSON.parse(sessionStorage.getItem("token")); if (!token) return; const { accessToken: { jwtToken }} = token; - const resp = await fetch(`${AUTH_URL}/auth/verify`, { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ token: jwtToken }) - }); - const result = await resp.json(); + const result = await auth.verify(jwtToken); if (result.authenticated) { setToken(token); } else { @@ -29,36 +22,12 @@ export const UserProvider = ({ children }) => { } }, []); - const requestSignIn = (username, password) => fetch(`${AUTH_URL}/auth/login`, { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - username, - password, - }) - }) - .then((response) => response.json()); - - const requestSignUp = (username, password) => fetch(`${AUTH_URL}/auth/register`, { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - username, - password, - }) - }) - .then((response) => response.json()); - const signIn = (username, password) => { if (!username) throw new Error('Email is required'); if (!password) throw new Error('Password is required'); setFetching(true); setError(null); - return requestSignIn(username, password) + return auth.signIn(username, password) .then((result) => { setFetching(false); if (result.message) throw new Error(result.message); @@ -78,7 +47,7 @@ export const UserProvider = ({ children }) => { if (password !== confirmPassword) throw new Error('Passwords do not match'); setFetching(true); setError(null); - return requestSignUp(username, password) + return auth.signUp(username, password) .then((result) => { if (result.message) throw new Error(result.message); }) diff --git a/src/services/auth.js b/src/services/auth.js new file mode 100644 index 0000000..9426bab --- /dev/null +++ b/src/services/auth.js @@ -0,0 +1,33 @@ +const AUTH_URL = 'https://dev-auth.fiskerdps.com'; + +export default { + signIn: (username, password) => fetch(`${AUTH_URL}/auth/login`, { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + username, + password, + }) + }).then((response) => response.json()), + + signUp: (username, password) => fetch(`${AUTH_URL}/auth/register`, { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + username, + password, + }) + }).then((response) => response.json()), + + verify: (jwt) => fetch(`${AUTH_URL}/auth/verify`, { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ token: jwt }) + }).then((response) => response.json()), +} \ No newline at end of file