This repository was archived by the owner on May 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathexamples.js
More file actions
90 lines (73 loc) · 3.33 KB
/
examples.js
File metadata and controls
90 lines (73 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// ES6 syntax is used here. Please refer to the link below for importing
// wstrade-api in CommonJS:
// https://github.com/ahmedsakr/wstrade-api/docs#importing-wstrade-api-commonjs-es6
import { auth } from 'wstrade-api';
/**
* Logging in requires a One-Time Password (OTP) event handler
* to be registered with the auth.on API.
*
* Below are two distinct examples of providing the OTP.
*/
// The OTP value here is a string literal. This will tell auth.login to automatically
// pass this value as part of the login request. It is assumed that you derived this
// OTP string literal manually.
auth.on('otp', '172829');
// The OTP value here is brought into the system automatically at run-time.
//
// This stubbed function is supposed to resemble a function that can
// connect to Gmail API and get the OTP code automatically.
const getCodeFromGmail = async () => {
let gmail_otp = null;
// logic to connect to gmail, get OTP and assign it to gmail_otp.
// You might want to have a sleep in here for 5 seconds to allow for Wealthsimple Trade
// to dispatch the OTP to your email.
return gmail_otp;
}
// getCodeFromGmail will be called by auth.login. Since it knows getCodeFromGmail is a function
// and not a string literal, auth.login will do the following:
//
// * attempt to login without OTP, failing to login but successfully dispatching an OTP
// * Invokes your OTP function to get the OTP (in this example, getCodeFromGmail)
// * Attempt to login for a second time with the OTP returned by getCodeFromGmail.
auth.on('otp', getCodeFromGmail);
// The 'refresh' authentication handler will be invoked when
// the existing tokens are refreshed. This could happen when you explicitly
// request a refresh through auth.tokens(), or it could happen implicitly
// if the existing access token has expired.
auth.on('refresh', (tokens) => {
console.log("My tokens were refreshed! Maybe i should store the new ones in my database...");
});
/**
* Now that we understand how to specify the OTP, let's talk about
* using the auth.login API.
*/
(async () => {
// To login, provide the email and password combination.
// auth.login will handle the OTP part according to how you configured
// it as discussed above.
await auth.login('example@hotmail.ca', 'mypassword');
// if you wanted to check the current authentication tokens, use the
// auth.tokens API
const currentTokens = auth.tokens();
// Output:
// {
// access: 'm8GKZp_wdnnae4JnqUmpNInZli-IkP9escCGcvwEsTQ',
// refresh: 'O3xScrMpYlPxdaDu2QM-yS-YlJS8s4jwZYZlHbt5RC0',
// expires: 1607137004
// };
// if you already have the authenticaiton tokens handy, you can use
// the auth.use API to load them in directly without having to log in
// again.
auth.use({
access: 'm8GKZp_wdnnae4JnqUmpNInZli-IkP9escCGcvwEsTQ',
refresh: 'O3xScrMpYlPxdaDu2QM-yS-YlJS8s4jwZYZlHbt5RC0',
expires: 1607137004
});
// If you wish to generate a new set of access and refresh tokens, you can do
// so by invoking auth.refresh. This will take your existing auth.tokens.refresh
// token and generate new access and refresh tokens from the Wealthsimple Trade servers.
//
// Keep in mind that wstrade-api does implicitly refresh access tokens when they expire.
await auth.refresh();
})()
.catch(console.log);