@@ -18,16 +18,22 @@ Specifying the `baseUrl` would be optional, and will contain `https://v4.passwor
18
18
19
19
To obtain your ` ApiSecret ` , you can find it in the admin console under ` Applications > 'Your Application' > Getting Started ` .
20
20
21
- ``` TSX
21
+ ``` tsx
22
22
const options: PasswordlessOptions = {
23
23
baseUrl: ' https://v4.passwordless.dev'
24
24
};
25
- this ._passwordlessClient = new PasswordlessClient (' demo:secret:f831e39c29e64b77aba547478a4b3ec6' , options );
25
+ this ._passwordlessClient = new PasswordlessClient (
26
+ ' demo:secret:f831e39c29e64b77aba547478a4b3ec6' ,
27
+ options
28
+ );
26
29
```
27
30
28
- ``` TSX
31
+ ``` tsx
29
32
const options: PasswordlessOptions = {};
30
- this ._passwordlessClient = new PasswordlessClient (' demo:secret:f831e39c29e64b77aba547478a4b3ec6' , options );
33
+ this ._passwordlessClient = new PasswordlessClient (
34
+ ' demo:secret:f831e39c29e64b77aba547478a4b3ec6' ,
35
+ options
36
+ );
31
37
```
32
38
33
39
### Registration
@@ -36,67 +42,68 @@ If you had for example a 'UserController.ts' with a 'signup' arrow function. You
36
42
37
43
You'll first want to proceed to store the new user in your database and verifying it has succeeded, before registering the token.
38
44
39
- ``` TSX
45
+ ``` tsx
40
46
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
+ };
77
84
```
78
85
79
86
### Logging in
80
87
81
- ``` TSX
88
+ ``` tsx
82
89
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 ;
99
101
}
102
+ } catch (error ) {
103
+ console .error (error .message );
104
+ }
105
+ response .send (401 );
106
+ };
100
107
```
101
108
102
109
## References
0 commit comments