Captain PreC
Convex Community2y ago
32 replies
Captain Pre

How Do You Handle Auth? I am trying to upload user data to the database after Google OAuth.

Alright, Here is the thing.
I have set up JWT Cookie Based auth using Auth Js, I simply want to store the User Data in the Database.

const session = await auth(); // Gives me session.user.name,  session.user.email,  session.user.image


I have a
users.tsx
:

import { mutation } from "./_generated/server";
import { v } from "convex/values";

export const createUser = mutation({
  args: { email: v.string(), name: v.string(), image: v.string() },
  handler: async (ctx, args) => {
    const userId = await ctx.db.insert("users", {
      email: args.email,
      name: args.name,
      image: args.image,
    });
    return userId;
    console.log(userId);
  },
});


SignIn Page :

import { signIn } from "@/auth";

export default function Home() {
  return (
    <>
      <form
        action={async () => {
          "use server";
          await signIn("google", { redirectTo: "/dashboard" });
        }}
      >
        <button type="submit">Signin with Google</button>
      </form>
    </>
  );
}



as you can see users are redirected to
/dashboard
after successful login, Upon which i want to call the createUser() function so the userdata is stored in the database.


Is this a good approach? If not, What is a simple way to make it work. Auth is already way too complicated for me. I dont want to use clerk as i have started to learn auth js and dont want to drop it.
Was this page helpful?