Skip to content

Instantly share code, notes, and snippets.

@seanmodd
Created August 19, 2021 19:01
Show Gist options
  • Save seanmodd/793db9a0dd31a6a7f187767a34de8c3f to your computer and use it in GitHub Desktop.
Save seanmodd/793db9a0dd31a6a7f187767a34de8c3f to your computer and use it in GitHub Desktop.
AuthContext for _app.js
import { createContext, useState, useEffect } from 'react';
import { useRouter } from 'next/router';
import { API_URL, NEXT_URL } from '@/config/index';
const AuthContext = createContext();
export const AuthProvider = ({ children }) => {
const [user, setUser] = useState(null);
const [error, setError] = useState(null);
const router = useRouter();
useEffect(() => checkUserLoggedIn(), []);
const signup = async (user) => {
console.log(user);
const res = await fetch(`${NEXT_URL}/api/signup`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(user),
});
const data = await res.json();
console.log('res', res);
console.log('data', data);
if (res.ok) {
setUser(data.user);
router.push('/auth/dashboard');
} else {
setError(data.message);
setError(null);
}
};
const signin = async ({ email: identifier, password }) => {
console.log({ identifier, password });
const res = await fetch(`${NEXT_URL}/api/signin`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
identifier,
password,
}),
});
const data = await res.json();
console.log('data', data);
if (res.ok) {
setUser(data.user);
router.push('/auth/dashboard');
} else {
setError(data.message);
setError(null);
}
};
const signout = async (user) => {
console.log(user);
// const res = await fetch(`${NEXT_URL}/api/signout`, {
// method: 'POST',
// });
// if (res.ok) {
// setUser(null);
// router.push('/');
// }
};
const checkUserLoggedIn = async (user) => {
console.log('check');
const res = await fetch(`${NEXT_URL}/api/user`);
const data = await res.json();
if (res.ok) {
setUser(data.user);
} else {
setUser(null);
}
};
return (
<AuthContext.Provider value={{ user, error, signin, signup, signout }}>
{children}
</AuthContext.Provider>
);
};
export default AuthContext;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment