-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
72 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |