Created
August 4, 2024 20:13
-
-
Save colinfwren/df1f340fc9c862793285d521cd21f6d5 to your computer and use it in GitHub Desktop.
Getting a JWT for a newly created user
This file contains hidden or 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
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