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
- Update User Error
```ts const { users, databases } = await createAdminClient(); const session = await getLoggedInUser(); const user = await users.get(session.$id); if (!use...
- apple exchange code to token
hello guys, im new here 🙂 I have created a project and enabled apple oauth, filled all data (client id, key id, p8 file itself etc). I generate oauth code form...
- How to Avoid Double Requests in function...
I'm currently using Appwrite's `functions.createExecution` in my project. I want to avoid double requests when multiple actions (like searching or pagination) a...