refactor: Auth

This commit is contained in:
Anoop M D 2022-03-08 02:22:21 +05:30
parent aa98cfe86e
commit 5683298deb
7 changed files with 29 additions and 24 deletions

5
renderer/.env.prod Normal file
View File

@ -0,0 +1,5 @@
ENV=production
NEXT_PUBLIC_ENV=prod
NEXT_PUBLIC_GRAFNODE_SERVER_API=https://ada.grafnode.com/api

13
renderer/api/auth.js Normal file
View File

@ -0,0 +1,13 @@
import { get, post, put } from './base';
const AuthApi = {
whoami: () =>get('auth/v1/user/whoami'),
signup: (params) =>post('auth/v1/user/signup', params),
login: (params) =>post('auth/v1/user/login', params),
signout: () => post('auth/v1/user/logout'),
getProfile: () =>get('auth/v1/user/profile'),
updateProfile: (params) =>put('auth/v1/user/profile', params),
updateUsername: (params) =>put('auth/v1/user/username', params)
};
export default AuthApi;

View File

@ -1,7 +1,7 @@
import axios from "axios";
const apiClient = axios.create({
baseURL: process.env.NEXT_PUBLIC_API
baseURL: process.env.NEXT_PUBLIC_GRAFNODE_SERVER_API
});
apiClient.interceptors.request.use((config) => {

View File

@ -1,13 +0,0 @@
import { get, post, put } from './base';
const IdentityApi = {
whoami: () =>get('v1/user/whoami'),
signup: (params) =>post('v1/user/register', params),
login: (params) =>post('v1/user/login', params),
signout: () => post('v1/user/logout'),
getProfile: () =>get('v1/user/profile'),
updateProfile: (params) =>put('v1/user/profile', params),
updateUsername: (params) =>put('v1/user/username', params)
};
export default IdentityApi;

View File

@ -3,7 +3,7 @@ import * as Yup from 'yup';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { useAuth } from 'providers/Auth';
import IdentityApi from 'api/identity';
import AuthApi from 'api/auth';
import { useFormik } from 'formik';
import StyledWrapper from './StyledWrapper';
@ -31,7 +31,7 @@ const Login = () => {
}),
onSubmit: (values, { resetForm }) => {
setLoggingIn(true);
IdentityApi
AuthApi
.login({
email: values.email,
password: values.password
@ -54,7 +54,7 @@ const Login = () => {
};
if(currentUser) {
router.push('/home');
router.push('/');
return null;
};

View File

@ -1,7 +1,7 @@
import React, { useState } from "react";
import Link from 'next/link';
import StyledWrapper from './StyledWrapper';
import IdentityApi from 'api/identity';
import AuthApi from 'api/auth';
import { useFormik } from 'formik';
import * as Yup from 'yup';
import { useRouter } from 'next/router';
@ -39,7 +39,7 @@ const SignUp = () => {
}),
onSubmit: (values, { resetForm }) => {
setSigningUp(true);
IdentityApi
AuthApi
.signup({
name: values.name,
email: values.email,
@ -54,7 +54,7 @@ const SignUp = () => {
.catch((error) => {
setSigningUp(false);
setErrorSigningUp(true);
setErrorMsg(error.message)
setErrorMsg(error.message || 'An error occured during signup')
});
setSigningUp(false);
},
@ -162,7 +162,7 @@ const SignUp = () => {
</div>
) :
<div>
{errorSigningUp && errorMsg.length ? (
{errorSigningUp ? (
<div className="field-error error-msg mb-2 text-red-500 ml-1 mt-1">{errorMsg}</div>
) : null}
<div className="text-center pt-4">

View File

@ -1,6 +1,6 @@
import React, { useEffect, useReducer } from 'react';
import { useRouter } from 'next/router';
import IdentityApi from 'api/identity';
import AuthApi from 'api/auth';
import reducer from './reducer';
const AuthContext = React.createContext();
@ -16,7 +16,7 @@ export const AuthProvider = props => {
const [state, dispatch] = useReducer(reducer, initialState);
useEffect(() => {
IdentityApi
AuthApi
.whoami()
.then((response) => {
let data = response.data;
@ -39,7 +39,7 @@ export const AuthProvider = props => {
useEffect(() => {
if(state.lastStateTransition === 'LOGIN_SUCCESS') {
router.push('/home');
router.push('/');
}
if(state.lastStateTransition === 'WHOAMI_ERROR') {
// Todo: decide action