Skip to content

Commit d602ca8

Browse files
authored
fixed syntax highlighting. (#103)
1 parent 7a7292c commit d602ca8

File tree

1 file changed

+65
-58
lines changed

1 file changed

+65
-58
lines changed

src/guide/backend/nodejs.md

+65-58
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ Specifying the `baseUrl` would be optional, and will contain `https://v4.passwor
1818

1919
To obtain your `ApiSecret`, you can find it in the admin console under `Applications > 'Your Application' > Getting Started`.
2020

21-
```TSX
21+
```tsx
2222
const options: PasswordlessOptions = {
2323
baseUrl: 'https://v4.passwordless.dev'
2424
};
25-
this._passwordlessClient = new PasswordlessClient('demo:secret:f831e39c29e64b77aba547478a4b3ec6', options);
25+
this._passwordlessClient = new PasswordlessClient(
26+
'demo:secret:f831e39c29e64b77aba547478a4b3ec6',
27+
options
28+
);
2629
```
2730

28-
```TSX
31+
```tsx
2932
const options: PasswordlessOptions = {};
30-
this._passwordlessClient = new PasswordlessClient('demo:secret:f831e39c29e64b77aba547478a4b3ec6', options);
33+
this._passwordlessClient = new PasswordlessClient(
34+
'demo:secret:f831e39c29e64b77aba547478a4b3ec6',
35+
options
36+
);
3137
```
3238

3339
### Registration
@@ -36,67 +42,68 @@ If you had for example a 'UserController.ts' with a 'signup' arrow function. You
3642

3743
You'll first want to proceed to store the new user in your database and verifying it has succeeded, before registering the token.
3844

39-
```TSX
45+
```tsx
4046
signup = async (request: express.Request, response: express.Response) => {
41-
const signupRequest: SignupRequest = request.body;
42-
const repository: UserRepository = new UserRepository();
43-
let id: string = null;
44-
try {
45-
// First, create the user in your database. We will use the id to register the token.
46-
// This way we know what user the credentials will belong to.
47-
id = repository.create(signupRequest.username, signupRequest.firstName, signupRequest.lastName);
48-
} catch {
49-
// do error handling, creating user failed.
50-
} finally {
51-
repository.close();
52-
}
53-
54-
if (!id) {
55-
// Do not proceed to create a token, we failed to create a user.
56-
response.send(400);
57-
}
58-
59-
let registerOptions = new RegisterOptions();
60-
registerOptions.userId = id;
61-
registerOptions.username = signupRequest.username;
62-
63-
// We will use our deviceName as an alias. But using an alias is completely optional.
64-
if (signupRequest.deviceName) {
65-
registerOptions.aliases = new Array(1);
66-
registerOptions.aliases[0] = signupRequest.deviceName;
67-
}
68-
69-
registerOptions.discoverable = true;
70-
71-
// Now call Passwordless.dev to register a new token.
72-
const token: RegisterTokenResponse = await this._passwordlessClient.createRegisterToken(registerOptions);
73-
74-
// Return the token to the client.
75-
response.send(token);
76-
}
47+
const signupRequest: SignupRequest = request.body;
48+
const repository: UserRepository = new UserRepository();
49+
let id: string = null;
50+
try {
51+
// First, create the user in your database. We will use the id to register the token.
52+
// This way we know what user the credentials will belong to.
53+
id = repository.create(signupRequest.username, signupRequest.firstName, signupRequest.lastName);
54+
} catch {
55+
// do error handling, creating user failed.
56+
} finally {
57+
repository.close();
58+
}
59+
60+
if (!id) {
61+
// Do not proceed to create a token, we failed to create a user.
62+
response.send(400);
63+
}
64+
65+
let registerOptions = new RegisterOptions();
66+
registerOptions.userId = id;
67+
registerOptions.username = signupRequest.username;
68+
69+
// We will use our deviceName as an alias. But using an alias is completely optional.
70+
if (signupRequest.deviceName) {
71+
registerOptions.aliases = new Array(1);
72+
registerOptions.aliases[0] = signupRequest.deviceName;
73+
}
74+
75+
registerOptions.discoverable = true;
76+
77+
// Now call Passwordless.dev to register a new token.
78+
const token: RegisterTokenResponse =
79+
await this._passwordlessClient.createRegisterToken(registerOptions);
80+
81+
// Return the token to the client.
82+
response.send(token);
83+
};
7784
```
7885

7986
### Logging in
8087

81-
```TSX
88+
```tsx
8289
signin = async (request: express.Request, response: express.Response) => {
83-
try {
84-
const token: string = request.query.token as string;
85-
86-
// First check if the token is valid, and if a matching user is found.
87-
const verifiedUser: VerifiedUser = await this._passwordlessClient.verifyToken(token);
88-
89-
// If a user is found, and the token is valid, you can proceed to log the user in.
90-
if (verifiedUser && verifiedUser.success === true) {
91-
// If you want to build a JWT token for SPA that are rendered client-side, you can do this here.
92-
response.send(JSON.stringify(verifiedUser));
93-
return;
94-
}
95-
} catch (error) {
96-
console.error(error.message);
97-
}
98-
response.send(401);
90+
try {
91+
const token: string = request.query.token as string;
92+
93+
// First check if the token is valid, and if a matching user is found.
94+
const verifiedUser: VerifiedUser = await this._passwordlessClient.verifyToken(token);
95+
96+
// If a user is found, and the token is valid, you can proceed to log the user in.
97+
if (verifiedUser && verifiedUser.success === true) {
98+
// If you want to build a JWT token for SPA that are rendered client-side, you can do this here.
99+
response.send(JSON.stringify(verifiedUser));
100+
return;
99101
}
102+
} catch (error) {
103+
console.error(error.message);
104+
}
105+
response.send(401);
106+
};
100107
```
101108

102109
## References

0 commit comments

Comments
 (0)