auth works now
This commit is contained in:
@ -2,7 +2,7 @@ import { createContext, useState, useEffect } from "react";
|
||||
import jwt_decode from "jwt-decode";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
const AuthContext = createContext("");
|
||||
const AuthContext = createContext();
|
||||
|
||||
export default AuthContext;
|
||||
|
||||
@ -25,8 +25,7 @@ export const AuthProvider = ({ children }) => {
|
||||
const response = await fetch("http://127.0.0.1/api/auth/token/", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
@ -36,30 +35,29 @@ export const AuthProvider = ({ children }) => {
|
||||
const data = await response.json();
|
||||
|
||||
if (response.status === 200) {
|
||||
console.log(response, data)
|
||||
setAuthTokens(data);
|
||||
setUser(jwt_decode(data.access));
|
||||
localStorage.setItem("authTokens", JSON.stringify(data));
|
||||
history.push("/");
|
||||
history("/");
|
||||
} else {
|
||||
alert("Something went wrong!");
|
||||
}
|
||||
};
|
||||
|
||||
const registerUser = async (username, password) => {
|
||||
const registerUser = async (username, password, password2) => {
|
||||
const response = await fetch("http://127.0.0.1/api/auth/register/", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
password
|
||||
password,
|
||||
password2
|
||||
})
|
||||
});
|
||||
if (response.status === 201) {
|
||||
history.push("/login");
|
||||
history("/login");
|
||||
} else {
|
||||
alert("Something went wrong!");
|
||||
}
|
||||
@ -69,7 +67,7 @@ export const AuthProvider = ({ children }) => {
|
||||
setAuthTokens(null);
|
||||
setUser(null);
|
||||
localStorage.removeItem("authTokens");
|
||||
history.push("/");
|
||||
history("/");
|
||||
};
|
||||
|
||||
const contextData = {
|
||||
|
||||
Reference in New Issue
Block a user