O

ouhammmourachid / auth-profiles

Public

Every signup gets a public profile with a unique username, avatar and bio. Works with email/password and OAuth2 signups.

  • Auth
  • 0 deploys
  • 0 forks

Auth + Public Profiles

Every signup gets a public profile with a unique username, avatar and bio. Works with email/password and OAuth2 signups.

What you get

  • A profile is created automatically for every new user, including OAuth2 signups
  • Usernames are unique, lowercase, and generated from the user's name or email
  • Reserved usernames (admin, api, support...) are refused
  • Profiles are public to read; only the owner can edit theirs
  • Avatars with 100x100 and 400x400 thumbnails
  • Leaves the built-in users collection untouched, so private fields like email stay private

Collections

profiles

Created by a hook when a user signs up; deleted with the user.

FieldTypeNotes
user (required)relation → usersOne profile per user
username (required)textUnique, a-z 0-9 _
display_nametext
avatarfileImages up to 5 MB
biotext
websiteurl
locationtext
ActionWho can do it
List / viewanyone
Createsuperusers only
Updateuser = @request.auth.id
Deletesuperusers only

Hooks

All hooks are in pb_hooks/main.pb.js and commented.

Setup

  • Turn on the OAuth2 providers you want under users > Options > OAuth2.

Quick start

import PocketBase from "pocketbase"
const pb = new PocketBase("https://YOUR-INSTANCE.pocketshub.com")

await pb.collection("users").create({ email, password, passwordConfirm: password, name: "Sam Lee" })
await pb.collection("users").authWithPassword(email, password)

const me = await pb.collection("profiles").getFirstListItem(`user = "${pb.authStore.record.id}"`)
await pb.collection("profiles").update(me.id, { username: "samlee", bio: "Building things." })

// Public profile page:
const profile = await pb.collection("profiles").getFirstListItem(`username = "samlee"`)

License

MIT

profiles
  • id
  • usersingle
  • username
  • display_name
  • avatarsingle
  • bio
  • website
  • location
  • created
  • updated

1 collection, 0 relations

main.pb.js48 lines
/// <reference path="../pb_data/types.d.ts" />

// Every new user (password or OAuth2 signup) gets a public profile with a unique username.
onRecordAfterCreateSuccess((e) => {
  const user = e.record
  const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"

  let base = (user.getString("name") || user.email().split("@")[0] || "")
    .toLowerCase()
    .replace(/[^a-z0-9_]/g, "")
    .slice(0, 20)
  if (base.length < 3) base = "user" + $security.randomStringWithAlphabet(4, alphabet)

  let username = base
  for (let i = 0; i < 10; i++) {
    if (e.app.countRecords("profiles", $dbx.hashExp({ username: username })) === 0) break
    username = base + "_" + $security.randomStringWithAlphabet(4, alphabet)
  }

  const profile = new Record(e.app.findCollectionByNameOrId("profiles"))
  profile.set("user", user.id)
  profile.set("username", username)
  profile.set("display_name", user.getString("name"))
  e.app.save(profile)

  e.next()
}, "users")

// Owners edit their own profile. The user link can't change, and some usernames are reserved.
onRecordUpdateRequest((e) => {
  if (!e.hasSuperuserAuth()) {
    if (e.record.original().getString("user") !== e.record.getString("user")) {
      throw new ForbiddenError("A profile can't be moved to another user.")
    }
  }

  const reserved = [
    "admin", "administrator", "api", "app", "auth", "billing", "help", "login", "logout",
    "me", "null", "root", "settings", "signup", "support", "system", "undefined", "www",
  ]
  const username = e.record.getString("username").toLowerCase()
  e.record.set("username", username)
  if (reserved.indexOf(username) !== -1) {
    throw new BadRequestError(`The username "${username}" is reserved.`)
  }

  e.next()
}, "profiles")