How to get current user in function?

Hi there,

i am writing a function (Serverless Functions (API Endpoints) | RedwoodJS Docs) where it would be nice to get the current user. how would i do that?
getCurrentUser needs a decoded session, but where do i get this?

thanks a lot,

pcace

Hi @pcace have a look at the authentication section of these docs Serverless Functions (API Endpoints) | RedwoodJS Docs

You can wrap the function in a requireAuth which lets you get the current user. There is an example.

How to Secure a Function with Redwood Auth

Hm,
i think i do not exaclty get the documentation - i have build my code exactly as in the documentation. the part i dont understand is how i get to the user part. i import the function getCurrentUser, but in order to use it, i need to have the decoded session. where does that come from?

Thanks a lot!!

auth.ts(26, 38): An argument for 'session' was not provided.
(alias) getCurrentUser(session: Decoded): Promise<{
    id: string;
    email: string;
    firstName: string;
    lastName: string;
    picture: string | null;
    preferences: JsonValue;
    members: {
        id: string;
        role: $Enums.Role;
        tenant: {
            ...;
        };
        teams: {
            ...;
        }[];
    }[];
}>
import getCurrentUser

Hi, there, i think i have found a little bit more what i dont understand :wink:

when following this: Serverless Functions (API Endpoints) | RedwoodJS Docs

i wonder how good that works in development environment without ssl. whenever i use the
if (isAuthenticated()) {
part, i get cors errors.

is there any way to get the example to run in non https environment, but still check isAuthenticated?

Thanks a lot!