Back

OAuth session cookie is set but wont delete

  • 0
  • Self Hosted
  • Auth
  • Web
Its_MS
8 May, 2024, 21:17

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?

TypeScript
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.

TL;DR
OAuth session cookie is not being read, leading to errors with fetching user details. The issue might lie in the cookie not being set or deleted correctly, causing redirection problems. Check the handling of the cookie in the code.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more