Skip to content

Instantly share code, notes, and snippets.

@colinfwren
Created August 4, 2024 20:13
Show Gist options
  • Save colinfwren/df1f340fc9c862793285d521cd21f6d5 to your computer and use it in GitHub Desktop.
Save colinfwren/df1f340fc9c862793285d521cd21f6d5 to your computer and use it in GitHub Desktop.
Getting a JWT for a newly created user
async function getNewUser(): Promise<{ user: Models.User<Models.Preferences>, jwt: string}> {
const client = new Client()
.setEndpoint(APPWRITE_URL)
.setProject(APPWRITE_PROJECT)
.setKey(APPWRITE_API_KEY)
const users = new Users(client)
const userEmail = `test-user-${randomUUID()}@test.com`
const user = await users.create(ID.unique(), userEmail, undefined, NEW_USER_PASSWORD, userEmail)
const agent = request.agent(APPWRITE_URL)
await agent
.post('/account/sessions/email')
.set('X-Appwrite-Response-Format', '1.4.0')
.set('X-Appwrite-Project', APPWRITE_PROJECT)
.send({
'email': userEmail,
'password': NEW_USER_PASSWORD
})
const jwtResponse = await agent
.post('/account/jwt')
.set('X-Appwrite-Response-Format', '1.4.0')
.set('X-Appwrite-Project', APPWRITE_PROJECT)
return {
user,
jwt: jwtResponse.body?.jwt
}
}
test('how you use it', async () => {
const { user, jwt } = await getNewUser()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment