Login user through the backend

Is there a way to trigger user login through a server side(backend) login function? My usecase is that I have a wasp app running in an iframe of a parent app and I want a user to login if a certain user role is passed from the parent app to the wasp app.
3 Replies
kapa.ai
kapa.ai4w ago
Hi @Dimitrios Mastrogiannis. 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 provide built-in hooks or methods to trigger user login directly from the server side. The authentication flow in Wasp is primarily designed for client-side initiation. Based on the knowledge sources, here are your options: 1. Custom Authentication Flow: You could create your own custom authentication flow by implementing your own login action. Wasp provides the ability to use the login() function from the client side as shown in the documentation:
import { login } from 'wasp/client/auth'

async function handleLogin() {
try {
await login(username, password)
// Redirect on successful login
} catch (error) {
// Handle error
}
}
import { login } from 'wasp/client/auth'

async function handleLogin() {
try {
await login(username, password)
// Redirect on successful login
} catch (error) {
// Handle error
}
}
2. Custom API Endpoint: You could create a custom API endpoint in your Wasp app that receives authentication data from the parent app and then triggers the login process. This would require some custom implementation. 3. JWT-based Authentication: Some users have implemented JWT-based authentication for similar use cases. You could create a custom API endpoint that generates a JWT token for authentication:
// main.wasp
api generateJwt {
fn: import { generateJwt } from "@src/server/api/generateJwt.js",
entities: [User],
httpRoute: (POST, "/api/generate-jwt")
}
// main.wasp
api generateJwt {
fn: import { generateJwt } from "@src/server/api/generateJwt.js",
entities: [User],
httpRoute: (POST, "/api/generate-jwt")
}
It's worth noting that Wasp is planning to add hooks for authentication in the future. As mentioned in one of the Discord threads:
"We will be adding hooks where you will be able to run code after/before signup/login, but yeah we don't have that yet unfortunately!" Wasp Discord
The timeline mentioned was "1-2 months" as of that Discord message, but it's unclear when exactly these features will be available. For your specific iframe use case, you might need to implement a custom solution using postMessage communication between the parent app and the iframe, where the parent app sends the user credentials to the iframe, and then the iframe uses the client-side login function to authenticate the user. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Dimitrios Mastrogiannis, 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?
Dimitrios Mastrogiannis
if i have managed to create a session at the server, how do i pass it to the client side?
miho
miho3w ago
What I wrote to you in the DM stands as far as the intial question goes:
There are useful functions you can call from wasp/auth/utils import { createUser, findAuthIdentity, ProviderName } from "wasp/auth/utils"; Check how Wasp does it internally and you can see what you maybe can use for your own impl in .wasp/out/server/src/auth/providers/username/login.ts
Could you explain your use case in more details? If you need the session on the client side, why can't just use the regular auth flow that logins in from the client?

Did you find this page helpful?