Move api calls into services
This commit is contained in:
@@ -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);
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user