Skip to content

Commit df120fc

Browse files
committed
firebase token generation
1 parent 8e1ed1a commit df120fc

File tree

6 files changed

+66
-15
lines changed

6 files changed

+66
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,4 @@ tests/cases/user/prettier/prettier
101101

102102
# Added
103103
Program.js
104+
firebase.json

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99
"author": "Brice Friha",
1010
"license": "ISC",
1111
"dependencies": {
12+
"@types/node": "^16.11.11",
13+
"@types/yargs": "^17.0.7",
14+
"googleapis": "^92.0.0",
1215
"node-fetch": "^2.6.6",
1316
"save-dev": "^0.0.1-security",
1417
"ts-node": "^10.4.0",
15-
"tslint": "^6.1.3"
18+
"tslint": "^6.1.3",
19+
"typescript": "^4.5.2",
20+
"yargs": "^17.3.0"
1621
},
1722
"devDependencies": {
1823
"@types/node-fetch": "^2.5.12"

src/controllers/Checker.ts

+45-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import Token from "../Models/Token";
22
import fetch from "node-fetch";
3+
import { google } from "googleapis";
4+
import key from "../placeholders/firebase.json";
35

6+
const PROJECT_ID = "<YOUR-PROJECT-ID>";
7+
const HOST = "fcm.googleapis.com";
8+
const PATH = "/v1/projects/" + PROJECT_ID + "/messages:send";
9+
const MESSAGING_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
10+
const SCOPES = [MESSAGING_SCOPE];
411
export default class Checker {
512
// List or all the crypto symbols that need to be checked
613
// private _symbols: Array<string>;
@@ -15,10 +22,10 @@ export default class Checker {
1522
// public set tokens (v : Array<Token>) {
1623
// this._tokens = v;
1724
// }
18-
1925
constructor() {
2026
this._interval = setInterval(() => {});
2127
clearInterval(this._interval);
28+
2229
// All the crypto symbols that need to be checked
2330
this._tokens = [
2431
{
@@ -147,15 +154,16 @@ export default class Checker {
147154
clearInterval(this._interval);
148155
}
149156
public SendNotification(title: string, msg: string) {
150-
fetch(
151-
`https://fcm.googleapis.com/v1/projects/cryptobob-eaff8/messages:send`,
152-
{
153-
method: "POST",
154-
headers: {
155-
Authorization:
156-
"Bearer ya29.a0ARrdaM9zZcAUyb0bT2EDZJ1YUNDB4RyJGmlyN1RvsBg-WZbj0xgadGed4mBqI3PtHzkn34pb7endIAku7Nikf4pqhWGDaAUKu7HeBiwt6una1fi2_IwmNVkWpjB3KuL18oJ9vLWmHCGnq5-OLIwbNI9Pr77k",
157-
},
158-
body: `
157+
this.GetToken().then(async (accessToken) => {
158+
console.log(accessToken);
159+
fetch(
160+
`https://fcm.googleapis.com/v1/projects/cryptobob-eaff8/messages:send`,
161+
{
162+
method: "POST",
163+
headers: {
164+
Authorization: `Bearer ${accessToken}`,
165+
},
166+
body: `
159167
{
160168
"message": {
161169
"notification": {
@@ -167,9 +175,33 @@ export default class Checker {
167175
}
168176
}
169177
`,
170-
}
171-
).then(async (res) => {
172-
console.log(res.status);
178+
}
179+
).then(async (res) => {
180+
console.log(res.status);
181+
});
182+
});
183+
}
184+
185+
private GetToken() {
186+
return new Promise(function (resolve, reject) {
187+
///const key = require(".../placeholders/firebase.json");
188+
const jwtClient = new google.auth.JWT(
189+
key.client_email,
190+
"",
191+
key.private_key,
192+
SCOPES,
193+
""
194+
);
195+
jwtClient.authorize(async (err, tokens) => {
196+
if (tokens) {
197+
if (err) {
198+
reject(err);
199+
return;
200+
}
201+
202+
resolve(tokens.access_token);
203+
}
204+
});
173205
});
174206
}
175207
}

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ import Checker from "./controllers/Checker";
1212
// });
1313
let checker = new Checker();
1414
checker.Run();
15-
//checker.SendNotification("test", "ts");
15+
checker.SendNotification("test", "ts");
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "cryptobob-eaff8",
4+
"private_key_id": "",
5+
"private_key": "",
6+
"client_email": "",
7+
"client_id": "",
8+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
9+
"token_uri": "https://oauth2.googleapis.com/token",
10+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11+
"client_x509_cert_url": ""
12+
}

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"resolveJsonModule": true,
34
/* Visit https://aka.ms/tsconfig.json to read more about this file */
45

56
/* Basic Options */

0 commit comments

Comments
 (0)