Skip to content

Commit

Permalink
wip: add session and user models
Browse files Browse the repository at this point in the history
  • Loading branch information
juni0r committed Nov 6, 2023
1 parent dd2bb4a commit fb36ef3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 46 deletions.
46 changes: 46 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { defineModel, zod, $ } from '.'
import { SupamodelError } from './errors'
import User from './user'
import { asData, failWith } from './util'

export interface Credentials {
email: string
password: string
}

const { custom, string, number } = zod

export class Session extends defineModel({
access_token: $(string()),
tokenType: $(string()),
expiresIn: $(number()),
expiresAt: $(number()),
refreshToken: $(string()),
user: $(
custom<User>((val: unknown) => val instanceof User, {
message: 'Invalid User',
params: { code: 'invalid-user' },
}),
{
take: (data) => User.take(data),
emit: (user) => user.$emit(),
},
),
}) {
static async signInWithPassword(credentials: Credentials) {
const { data, error } =
await this.client.auth.signInWithPassword(credentials)
if (error) throw error

// console.dir(data, { depth: 6 })
return this.take(data.session)
}

static async getUser() {
const { data, error } = await this.client.auth.getUser()
if (error) return failWith(SupamodelError, error)

return asData(User.take(data.user))
}
}
export default Session
51 changes: 5 additions & 46 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,13 @@
import 'dotenv/config'
import './luxon.inspect.custom'
import { defineModel, datetime, transform, $, zod, configureSupamodel } from '.'
import Session from './session'
import { configureSupamodel } from '.'

configureSupamodel({})

interface Credentials {
email: string
password: string
}

const { object, string } = zod

class User extends defineModel({
id: $(string()),
email: $(string().email()),
aud: $(string()),
role: $(string()),
emailConfirmedAt: $(datetime(), transform.datetime),
phone: $(string()),
confirmedAt: $(datetime(), transform.datetime),
recoverySentAt: $(datetime(), transform.datetime),
lastSignInAt: $(datetime(), transform.datetime),
appMetadata: $(object({}).passthrough()),
userMetadata: $(object({}).passthrough()),
identities: $(object({}).passthrough().array()),
createdAt: $(datetime(), transform.datetime),
updatedAt: $(datetime(), transform.datetime),
}) {
static async signInWithPassword(credentials: Credentials) {
const { data, error } =
await this.client.auth.signInWithPassword(credentials)
if (error) throw error

// console.dir(data, { depth: 6 })
return this.take(data.user)
}

static async instance() {
const { data, error } = await this.client.auth.getUser()
if (error) throw error

return this.take(data.user)
}
}

User.signInWithPassword({
Session.signInWithPassword({
email: 'andreas.korth@gmail.com',
password: 'rosebush',
}).then(async (user) => {
console.log(user.toJSON())
console.log('\ninstance:')
console.log((await User.instance()).toJSON())
}).then(async (session) => {
console.dir(session.toJSON(), { depth: 5 })
})
21 changes: 21 additions & 0 deletions src/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineModel, datetime, transform, zod, $ } from '.'

const { object, string } = zod

export class User extends defineModel({
id: $(string()),
email: $(string().email()),
aud: $(string()),
role: $(string()),
emailConfirmedAt: $(datetime(), transform.datetime),
phone: $(string()),
confirmedAt: $(datetime(), transform.datetime),
recoverySentAt: $(datetime(), transform.datetime),
lastSignInAt: $(datetime(), transform.datetime),
appMetadata: $(object({}).passthrough()),
userMetadata: $(object({}).passthrough()),
identities: $(object({}).passthrough().array()),
createdAt: $(datetime(), transform.datetime),
updatedAt: $(datetime(), transform.datetime),
}) {}
export default User

0 comments on commit fb36ef3

Please sign in to comment.