Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signup Authentication using JWT #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/api/Signup/signup-auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import axios from 'axios';

const signupAxios = axios.create({
baseURL: 'http://localhost:8080',
});

signupAxios.interceptors.request.use(
(config) => {
const token = localStorage.getItem('jwt');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => {
return Promise.reject(error);
}
);

export default signupAxios;
24 changes: 22 additions & 2 deletions src/components/signup/SignupForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ import { Button, Grid, Link, TextField, Typography } from '@mui/material';
import { Box } from '@mui/system';
import { ChangeEvent, FormEvent, useState } from 'react';
import { Link as RouterLink, useParams } from 'react-router-dom';

import './auth/signup-auth';
import { green, grey } from '@mui/material/colors';

import customAxios from '../../api/Signup/signup-auth';
interface FormData {
lastname: string;
firstname: string;
email: string;
password: string;
role: string[];
}

interface FormErrors {
lastname?: string;
firstname?: string;
email?: string;
password?: string;
role?: string[];
}

const SignupForm = () => {
Expand All @@ -28,14 +30,32 @@ const SignupForm = () => {
lastname: '',
email: '',
password: '',
role: [`${userType}`],
});

const handleSubmit = (e: FormEvent) => {
e.preventDefault();
if (validate()) {
console.log('Form Submitted: ', formData);
console.log(`user type: ${userType}`);

// POST REQUEST FOR THE BACKEND GOES HERE ....
const PostRequest = async () => {
try {
const response = await customAxios.post(
'/api/auth/register',
{
...formData,
},
{ withCredentials: true }
);
console.log('Signup Successful: ', response.data);
} catch (error) {
console.error('Signup Failed: ', error);
}
};
PostRequest();
//-------------------------//
}
};

Expand Down