Fix landpage loading

This commit is contained in:
Michael
2021-08-05 21:48:31 +02:00
parent 62ae8117e6
commit acd964a699
2 changed files with 32 additions and 27 deletions

View File

@@ -16,33 +16,11 @@ import Footer from "./Footer";
import Head from "next/head"; import Head from "next/head";
import Nav from "./Nav"; import Nav from "./Nav";
import { ToastContainer } from "react-toastify"; import { ToastContainer } from "react-toastify";
import { supabase } from "utils/supabaseClient";
import { useAuth } from "utils/AuthContext"; import { useAuth } from "utils/AuthContext";
import { useEffect } from "react";
const Layout = (props) => { const Layout = (props) => {
const { user, signOut } = useAuth(); const { user, signOut } = useAuth();
useEffect(() => {
const { data: authListener } = supabase.auth.onAuthStateChange(
(event, session) => {
if ((event === "SIGNED_OUT") | (event === "SIGNED_IN")) {
fetch("/api/auth", {
method: "POST",
headers: new Headers({ "Content-Type": "application/json" }),
credentials: "same-origin",
body: JSON.stringify({ event, session }),
}).then((res) => res.json());
}
if (event === "USER_UPDATED") {
}
}
);
return () => {
authListener.unsubscribe();
};
}, []);
const toastStyle = { const toastStyle = {
//Style your toast elements here //Style your toast elements here
success: "bg-accent", success: "bg-accent",

View File

@@ -1,4 +1,4 @@
import React, { createContext, useContext, useState } from "react"; import React, { createContext, useContext, useEffect, useState } from "react";
import { supabase } from "utils/supabaseClient"; import { supabase } from "utils/supabaseClient";
@@ -6,20 +6,47 @@ import { supabase } from "utils/supabaseClient";
const AuthContext = createContext(); const AuthContext = createContext();
export const AuthProvider = ({ children }) => { export const AuthProvider = ({ children }) => {
// create state values for user data and loading
const [user, setUser] = useState(); const [user, setUser] = useState();
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
// create signUp, signIn, signOut functions useEffect(() => {
// Check active sessions and sets the user
const session = supabase.auth.session();
setUser(session?.user ?? null);
setLoading(false);
// Listen for changes on auth state (logged in, signed out, etc.)
const { data: listener } = supabase.auth.onAuthStateChange(
async (event, session) => {
if ((event === "SIGNED_OUT") | (event === "SIGNED_IN")) {
fetch("/api/auth", {
method: "POST",
headers: new Headers({ "Content-Type": "application/json" }),
credentials: "same-origin",
body: JSON.stringify({ event, session }),
}).then((res) => res.json());
}
if (event === "USER_UPDATED") {
}
setUser(session?.user ?? null);
setLoading(false);
}
);
return () => {
listener?.unsubscribe();
};
}, []);
// Will be passed down to Signup, Login and Dashboard components
const value = { const value = {
signUp: (data) => supabase.auth.signUp(data), signUp: (data) => supabase.auth.signUp(data),
signIn: (data) => supabase.auth.signIn(data), signIn: (data) => supabase.auth.signIn(data),
signOut: () => supabase.auth.signOut(), signOut: () => supabase.auth.signOut(),
resetPassword: (data) => supabase.auth.api.resetPasswordForEmail(data),
user, user,
}; };
// use a provider to pass down the value
return ( return (
<AuthContext.Provider value={value}> <AuthContext.Provider value={value}>
{!loading && children} {!loading && children}