
I have made it so my users can log in with Github, and it does work, however when I try and get their details with account.get() I get the error AppwriteException: User (role: guests) missing scope (account)
I tried to do account.getSession('current') but I get the same error. I am using a custom Auth which I will add the code below. I believe the error has something to do with the cookie not being read so it doesn't correctly redirect my user?
import { useNavigate } from "react-router-dom";
import React, { createContext, useContext, useEffect, useState } from "react";
import { IUser } from "@/types";
import { getCurrentUser } from "@/lib/appwrite/api";
import { account } from "@/lib/appwrite/config";
import { OAuthProvider } from "appwrite";
export const INITIAL_USER = {
id: "",
name: "",
username: "",
email: "",
imageUrl: "",
};
const INITIAL_STATE = {
user: INITIAL_USER,
isLoading: false,
isAuthenticated: false,
setUser: () => {},
setIsAuthenticated: () => {},
checkAuthUser: async () => false as boolean,
};
type IContextType = {
user: IUser;
isLoading: boolean;
setUser: React.Dispatch<React.SetStateAction<IUser>>;
isAuthenticated: boolean;
setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>;
checkAuthUser: () => Promise<boolean>;
};
const AuthContext = createContext<IContextType>(INITIAL_STATE);
export function AuthProvider({ children }: { children: React.ReactNode }) {
const navigate = useNavigate();
const [user, setUser] = useState<IUser>(INITIAL_USER);
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const checkAuthUser = async () => {
setIsLoading(true);
try {
const currentAccount = await getCurrentUser();
if (currentAccount) {
setUser({
id: currentAccount.$id,
name: currentAccount.name,
username: currentAccount.username,
email: currentAccount.email,
imageUrl: currentAccount.imageURL,
});
setIsAuthenticated(true);
return true;
}
return false;
} catch (error) {
console.error(error);
return false;
} finally {
setIsLoading(false);
}
};
const handleGitHubLogin = () => {
account.createOAuth2Session(
OAuthProvider.Github,
"http://localhost:5173",
"http://localhost:5173/sign-in",
["user:email"]
);
};
useEffect(() => {
// const cookieFallback = localStorage.getItem("cookieFallback");
// if (
// cookieFallback === "[]" ||
// cookieFallback === null ||
// cookieFallback === undefined
// ) {
// navigate("/sign-in");
// }
checkAuthUser();
}, [navigate]);
const value = {
user,
setUser,
isLoading,
isAuthenticated,
setIsAuthenticated,
checkAuthUser,
handleGitHubLogin,
};
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}
export const useUserContext = () => useContext(AuthContext);
Any help would be appreciated.
Recommended threads
- Images not showing up --
so i made this movie app - i hosted it successfully using appwrite but the images arent showing up --- https://movie-app-jsm.appwrite.network/ this is the movie...
- What’s the current approach for always o...
Read that it’s not a thing out of the box, but what’s the possible setup here?
- Error 431 header fields too large
Some times to times when i load or switch the page, it shows an error 431. And on what i saw it'w because of the cookies, when i remove the legacy tokens it wo...
