/
AuthService.ts
97 lines (89 loc) · 2.18 KB
/
AuthService.ts
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
91
92
93
94
95
96
97
// PACKAGES
import { Auth } from 'rettiwt-auth';
// SERVICES
import { FetcherService } from '../internal/FetcherService';
// TYPES
import { IRettiwtConfig } from '../../types/RettiwtConfig';
/**
* Handles authentication.
*
* @public
*/
export class AuthService extends FetcherService {
/**
* @param config - The config object for configuring the Rettiwt instance.
*
* @internal
*/
public constructor(config?: IRettiwtConfig) {
super(config);
}
/**
* Login to twitter using account credentials.
*
* @param email - The email id associated with the Twitter account.
* @param userName - The username associated with the Twitter account.
* @param password - The password to the Twitter account.
* @returns The API_KEY for the Twitter account.
*
* @example
* ```
* import { Rettiwt } from 'rettiwt-api';
*
* // Creating a new Rettiwt instance
* const rettiwt = new Rettiwt();
*
* // Logging in an getting the API_KEY
* rettiwt.auth.login("email@domain.com", "username", "password")
* .then(apiKey => {
* // Use the API_KEY
* ...
* })
* .catch(err => {
* console.log(err);
* });
* ```
*/
public async login(email: string, userName: string, password: string): Promise<string> {
// Logging in and getting the credentials
let apiKey: string =
((
await new Auth({ proxyUrl: this.authProxyUrl }).getUserCredential({
email: email,
userName: userName,
password: password,
})
).toHeader().cookie as string) ?? '';
// Converting the credentials to base64 string
apiKey = Buffer.from(apiKey).toString('base64');
return apiKey;
}
/**
* Login to twitter as guest.
*
* @returns A new guest key.
*
* @example
* ```
* import { Rettiwt } from 'rettiwt-api';
*
* // Creating a new Rettiwt instance
* const rettiwt = new Rettiwt();
*
* // Logging in an getting a new guest key
* rettiwt.auth.guest()
* .then(guestKey => {
* // Use the guest key
* ...
* })
* .catch(err => {
* console.log(err);
* });
* ```
*/
public async guest(): Promise<string> {
// Getting a new guest key
const guestKey: string = (await new Auth().getGuestCredential()).guestToken ?? '';
return guestKey;
}
}