W
Wasp-lang•3mo ago
devrim

Implementing a user sign-in to multiple accounts, and placing an account switcher.

did someone do this before?
41 Replies
kapa.ai
kapa.ai•3mo ago
Hi @devrim. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Currently, Wasp doesn't support multiple auth identities for a single user. This means a user can't have both an email-based auth identity and a Google-based auth identity. However, this is a feature that is planned for the future with the introduction of the account merging feature. Account merging means that multiple auth identities can be merged into a single user account. For example, a user's email and Google identity can be merged into a single user account. Then the user can log in with either their email or Google account and they will be logged into the same account source (https://wasp-lang.dev/docs/auth/email). For social login options, Wasp currently supports Google, Github, and Keycloak. When a user signs in for the first time, Wasp creates a new user account and links it to the chosen auth provider account for future logins source (https://wasp-lang.dev/docs/auth/social-auth/overview). To implement social login with default settings, you can add the provider (e.g., google: {}, gitHub: {}, keycloak: {}) to the auth.methods dictionary in your main.wasp file source (https://wasp-lang.dev/docs/auth/social-auth/google#default-behaviour), source (https://wasp-lang.dev/docs/auth/social-auth/github#default-behaviour), source (https://wasp-lang.dev/docs/auth/social-auth/keycloak#default-behaviour). However, there is currently an open issue on Github discussing the implementation of better user account merging logic, which might be relevant to your question source (https://github.com/wasp-lang/wasp/issues/954). Please note that this is based on the current state of Wasp and may change in the future as new features are added. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @devrim, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
devrim
devrimOP•3mo ago
my question is @kapa.ai is not signing in to one account using different auth providers, having one person switch between multiple distinct accounts they have without logging out and logging in.
kapa.ai
kapa.ai•3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I'm sorry for misunderstanding your question earlier. As of now, there is no specific feature or documentation in Wasp that directly supports switching between multiple distinct user accounts without logging out and logging back in. The current authentication system in Wasp is designed to handle one user at a time per session. When a user logs in, a session is created and linked to that user. The session is then used to authenticate subsequent requests from the user source (https://wasp-lang.dev/docs/auth/entities#example-app-model). If you want to implement a feature that allows a user to switch between multiple accounts, you would likely need to manage multiple sessions and switch between them as needed. However, this would be a complex feature to implement and is not directly supported by Wasp at this time. Please note that this is based on the current state of Wasp and may change in the future as new features are added. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @devrim, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
neogeofun
neogeofun•3mo ago
Why would you need a user to sign in to multiple accounts? If they are multiple accounts, they should be separately credentialed. I cannot think of a good reason to implement a feature like this. Care to elaborate?
devrim
devrimOP•3mo ago
think of your google login, you have your @gmail then you have your @company.com mailboxes and google accounts associated with each is different. you log in to them separately but you don't log out from the previous one
neogeofun
neogeofun•3mo ago
Okay, so you want SSO?
devrim
devrimOP•3mo ago
i'm not sure if that's what that is called
neogeofun
neogeofun•3mo ago
Single sign on You'd have to write custom logic for that, I am not aware of SSO coming with Wasp out of box
devrim
devrimOP•3mo ago
no i don't think that is called SSO SSO is what okta does. you login once, they log you in to all other services on your behalf what we are talking about here is you have two separate logins to the same system one is personal, other one is a company account SSO is not required
neogeofun
neogeofun•3mo ago
ok walk me through i register with my gmail account using your app now i have another email that i want to use to login to the exact same account?
devrim
devrimOP•3mo ago
yes the one that is given and paid by a company you work at eg
MEE6
MEE6•3mo ago
Wohooo @devrim, you just became a Waspeteer level 3!
devrim
devrimOP•3mo ago
plus your personal
devrim
devrimOP•3mo ago
No description
neogeofun
neogeofun•3mo ago
it is not so straightforward but a naive solution would be implementing a table that allows you to store the other email of the user and then do a cross check during sign on add companyEmail field on user or userProfile table keep everything else the same, during sign on I can use either my company email or personal email, but i use the same password you check both columns in database
devrim
devrimOP•3mo ago
that won't do it. companies would like full control of company accounts, can't be connected to the other. good way to think about this, you have two sets of username/password, how can we switch between them without making user logout as in, you want to check your gmail, then want to check your work emails. you don't logout of gmail, you switch.
neogeofun
neogeofun•3mo ago
https://en.wikipedia.org/wiki/Single_sign-on Without a naive solution, that's a lot of work, good luck
devrim
devrimOP•3mo ago
no we're not talking about SSO. but thanks. maybe someone else made something similar, i'll stand by.
neogeofun
neogeofun•3mo ago
https://auth0.com/docs/api/management/v2/users/post-identities i wouldn't try to implement it from the scratch try to find an existing solution, even with that, it is not going to be easy to do it right
devrim
devrimOP•3mo ago
look i want to be polite, but SSO ain't it. if you could stop suggesting that that'd be great so others can chime in
neogeofun
neogeofun•3mo ago
lol ok
devrim
devrimOP•3mo ago
SSO in this case would be non-compliant, and a non-starter also not the problem. SSO means, i log in with my gmail, and that logs me in to my work emails.
neogeofun
neogeofun•3mo ago
you gave Gmail as an example, but whatever good luck
devrim
devrimOP•3mo ago
@sodic would you have an idea about this?
Filip
Filip•3mo ago
Hey, I think I get what you mean, you want the app to offer a dropdown with all the accounts you're logged in. I'm not sure how I'd go about implementing this (in Wasp or otherwise). Web apps sesions usually include a session key that only identifies a certain account. To do this, you'd have to keep both sessions alive (i.e., somehow change them without logging out the user), which Wasp doesn't rally support. Hmm, @devrim, can you give an example of a web app that does this, so I can go check it out. You mentioned google, but from what I know, they just open a new tab and log you into the other account. Is there an app that does it on the same page, or would this behavior be enough?
devrim
devrimOP•3mo ago
Hi @sodic yes you got it. there are many of them surely you know, Instagram Twitter slack to name a few. I found this article that goes over: https://medium.com/@Shiran/it-is-all-about-the-feature-experience-today-switching-accounts-3c3626675227
Medium
The story of switching accounts
I’ve just finished doing market research about the option to switch accounts. I want to share here some insights.
devrim
devrimOP•3mo ago
Usually when the auth completes with one account it sets a cookie, you write that cookie to local storage, with an expiration date, when user switches back, you set that cookie back so that user do not need to enter password
fustuk.
fustuk.•3mo ago
really difficult to implement imo. You have to keep all logged in accounts in session storage and display them in the dropdown, when user switches acc, you refresh the old token maybe? Other possible solution would be to connect 2 accounts on db level somehow and go from there, but again really hard to do properly without security breaches :/
devrim
devrimOP•3mo ago
This way we will know all accounts of the user, display in a list Yes but not that difficult. Just need to know what wasp is using to auth a user in the browser (is that a cookie? Or some react vars?)), and set that to local storage and set it back upon switch.
fustuk.
fustuk.•3mo ago
sure but also make sure those tokens will be valid for renewal after X months
No description
devrim
devrimOP•3mo ago
cool that's what i was asking for. so that sessionId establishes the auth?
fustuk.
fustuk.•3mo ago
Looks like it but @miho @sodic can confirm I deleted it and Im no longer logged in so this is what you are looking for, but again keep in mind you have to find out how long the tokens are valid for
devrim
devrimOP•3mo ago
also need to find out where it's being set, and hopefully we can get a hook back vs changing the wasp internals
fustuk.
fustuk.•3mo ago
import { setSessionId } from 'wasp/client/api'
import { invalidateAndRemoveQueries } from '../../client/operations/internal/resources.js'

// PRIVATE API
export async function initSession(sessionId: string): Promise<void> {
setSessionId(sessionId)
// We need to invalidate queries after login in order to get the correct user
// data in the React components (using `useAuth`).
// Redirects after login won't work properly without this.

// TODO(filip): We are currently removing all the queries, but we should
// remove only non-public, user-dependent queries - public queries are
// expected not to change in respect to the currently logged in user.
await invalidateAndRemoveQueries()
}
import { setSessionId } from 'wasp/client/api'
import { invalidateAndRemoveQueries } from '../../client/operations/internal/resources.js'

// PRIVATE API
export async function initSession(sessionId: string): Promise<void> {
setSessionId(sessionId)
// We need to invalidate queries after login in order to get the correct user
// data in the React components (using `useAuth`).
// Redirects after login won't work properly without this.

// TODO(filip): We are currently removing all the queries, but we should
// remove only non-public, user-dependent queries - public queries are
// expected not to change in respect to the currently logged in user.
await invalidateAndRemoveQueries()
}
Found this, you can import this setSessionId method in ur code and set the user you want this is the function itself
// PRIVATE API (sdk)
export function setSessionId(sessionId: string): void {
waspAppAuthSessionId = sessionId
storage.set(WASP_APP_AUTH_SESSION_ID_NAME, sessionId)
apiEventsEmitter.emit('sessionId.set')
}
// PRIVATE API (sdk)
export function setSessionId(sessionId: string): void {
waspAppAuthSessionId = sessionId
storage.set(WASP_APP_AUTH_SESSION_ID_NAME, sessionId)
apiEventsEmitter.emit('sessionId.set')
}
martinsos
martinsos•3mo ago
Just for clarification, since a convo above got a bit long: What we are talking about here is the ability to be logged-in with multiple accounts at the same time and switch between them easily via UI (e.g. account switcher widget)? Like Twitter or GCalendar offer? If so, this shouldn't be super hard to implement on our side, although it takes a bit of work. We will want to store multiple login details on the frontend (cookies), and track multiple sessions on the backend (maybe we alredy do I would need to check). Another question is can you (Wasp user) implement this on your own right now? I am not sure but even if you can I imagine it would be a bit finnicky, you would have to deal with our internal stuff. Let's see what @miho says, he knows the most about our Auth since he implemented most of it and will be able to answer much faster than me. In general I think this is quite an interesting idea, if we manage to flush it out a bit more here we can open a Github issue and track it there as a future task, to implement.
miho
miho•3mo ago
We keep the session ID in local storage. You could keep track of the session ID for each account somehow and just swap it out so things in Wasp keep working as Wasp expects them to. Let's take a look at auth solutions out there like Auth0, Supabase Auth, Clerk etc. I'm not sure they also offer this out of the box. For example, Clerk gives you low level APIs to manipulate sessions (list them, set the active one) and build with it: https://clerk.com/docs/custom-flows/multi-session-applications IMHO what I just talked about can be achieved by just manipulating the value in localStorage by the user. Wasp could of course, accept that this is a use case it wants to support and offer a higher level API like getSessions() or setActiveSession() or smth like that.
Custom Flows: Build a custom multi-session flow
Learn how to use the Clerk API to add multi-session handling to your application.
No description
devrim
devrimOP•3mo ago
>We keep the session ID in local storage. You could keep track of the session ID for each account somehow and just swap it out so things in Wasp keep working as Wasp expects them to. perfect. thanks @miho
martinsos
martinsos•3mo ago
@miho could you please create GH issue for this? Nothing too long, you can just repeat what you said here + link to this convo. It will give us a place to follow this idea, we don't have to commit to doing it though, just track it there.
miho
miho•3mo ago
GitHub
Multi-session support (account switching) · Issue #2265 · wasp-lang...
Some services allow users to easily switch between multiple accounts without logging out and logging in again. Think, Google and multiple different Google accounts in a drop down. Wasp Auth could s...
devrim
devrimOP•3mo ago
oh wow! didn't know you guys were this fast! (oops. edit, thought it was a PR 😄 my bad, still impressive though!)
martinsos
martinsos•3mo ago
Haha, wow for a moment I got excited there while reading your message, thought it is indeed a PR :D. Hah sorry just an issue for now :D. But let us know if you implement it in userland on your side, would be helpful to hear about any learnings you had while doing it, it would probably help us once we get to implementing it.
Want results from more Discord servers?
Add your server