Move api calls into services

This commit is contained in:
jwu-fisker
2021-01-06 09:23:19 -08:00
parent db75494dd8
commit 002bad0a91
2 changed files with 37 additions and 35 deletions

View File

@@ -1,6 +1,6 @@
import React, { useContext, useEffect, useState } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import auth from '../../services/auth';
const AUTH_URL = 'https://dev-auth.fiskerdps.com';
const UserContext = React.createContext(); const UserContext = React.createContext();
export const UserProvider = ({ children }) => { export const UserProvider = ({ children }) => {
@@ -13,14 +13,7 @@ export const UserProvider = ({ children }) => {
const token = JSON.parse(sessionStorage.getItem("token")); const token = JSON.parse(sessionStorage.getItem("token"));
if (!token) return; if (!token) return;
const { accessToken: { jwtToken }} = token; const { accessToken: { jwtToken }} = token;
const resp = await fetch(`${AUTH_URL}/auth/verify`, { const result = await auth.verify(jwtToken);
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ token: jwtToken })
});
const result = await resp.json();
if (result.authenticated) { if (result.authenticated) {
setToken(token); setToken(token);
} else { } 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) => { const signIn = (username, password) => {
if (!username) throw new Error('Email is required'); if (!username) throw new Error('Email is required');
if (!password) throw new Error('Password is required'); if (!password) throw new Error('Password is required');
setFetching(true); setFetching(true);
setError(null); setError(null);
return requestSignIn(username, password) return auth.signIn(username, password)
.then((result) => { .then((result) => {
setFetching(false); setFetching(false);
if (result.message) throw new Error(result.message); 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'); if (password !== confirmPassword) throw new Error('Passwords do not match');
setFetching(true); setFetching(true);
setError(null); setError(null);
return requestSignUp(username, password) return auth.signUp(username, password)
.then((result) => { .then((result) => {
if (result.message) throw new Error(result.message); if (result.message) throw new Error(result.message);
}) })

33
src/services/auth.js Normal file
View File

@@ -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()),
}