Skip to content
Back

How many instance of session.create in Activity per login?

  • 0
  • Auth
  • Web
Los Feliz
26 Mar, 2025, 01:22

should every user login via OAuth2 create two instances of session.create in the Activity tab? When I login, only one instance of session gets created in the Sessions tab, but I see that two events of session.create was called. Is that how it should be?

my code for user auth:

TypeScript
export const googleOAuthLogin = async () => {
    try {
        const baseUrl = window.location.origin

        const userAccount = account.createOAuth2Token(
            'google', 
            `${baseUrl}/user/me`,  
            `${baseUrl}/`, 
        )

        console.log('Success logging in with Google:', userAccount);

    } catch (error) {
        console.error('Error logging in with google:', error);
    }
}


export const handleOAuthSession = async (userId, secret) => {

    try {
        await account.createSession(userId, secret);
 
        const user = await account.get();
 
        return user;
    } catch (error) {
        console.error('Authentication failed:', error);
        throw error;
    }
};

This is Me.jsx:

TypeScript
    useEffect(() => {
        const checkingSessionStatus = async () => {
            try {
                const usr = await getAccount();
                if (usr) {
                    console.log('Session in progress.');
                setIsSessionInProgress(true);
                    setUser(usr);
           } else {
setIsSessionInProgress(false);
}
} catch (error) {
console.error(error);
}
};
checkingSessionStatus();
}, [])

//Authenticating User
useEffect(() => {
const params = new URLSearchParams(window.location.search);
const userId = params.get('userId');
const secret = params.get('secret');
const authenticateUser = async () => {
if (hasAuthenticated.current || !userId || !secret) return;
hasAuthenticated.current = true;
try {
 const authenticatedUser = await handleOAuthSession(userId, secret); 
setUser(authenticatedUser);
} catch (err) { console.error(err);}
};
if (!isSessionInProgress) authenticateUser();
}, [isSessionInProgress]);
TL;DR
Developers are trying to figure out if every user login via OAuth2 should create two instances of `session.create` in the Activity tab, as they are seeing double the events being called. The code provided indicates that one event is created during the oauth2 flow and another during session creation. The issue seems to be related to how the code is handling session creation and authentication. Solution: The problem might stem from how `userAccount` is being handled in `googleOAuthLogin`, ensuring that `baseUrl` is set correctly might resolve the discrepancy in event creation. Also, reviewing the logic in `handleOAuthSession` and `Me.jsx`
Steven
26 Mar, 2025, 01:59

Hmm ya I guess it is a little weird. The oauth2 flow creates one event. Then, the create session creates another event

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