1 | 'use strict'; |
||
2 | |||
3 | let firebase = require('firebase'); |
||
4 | let admin = require('firebase-admin'); |
||
5 | |||
6 | /** Class representing a firebase authentication */ |
||
7 | class FirebaseAuth { |
||
8 | |||
9 | /** |
||
10 | * Creates a firebaseAuth |
||
11 | * @params {Object} firebaseConfig - The config for firebase |
||
12 | * @params {Object} serviceKey - The service account for firebase |
||
13 | */ |
||
14 | constructor(firebaseConfig, serviceKey) { |
||
15 | this.firebase = firebase; |
||
16 | this.admin = admin; |
||
17 | this.firebase.initializeApp(firebaseConfig); |
||
18 | this.admin.initializeApp({credential: this.admin.credential.cert(serviceKey)}); |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * Signing in an user and tries to get their id token. |
||
23 | * |
||
24 | * @param {String} email - Email for the account |
||
25 | * @param {String} password - Password for the account |
||
26 | * @promise {String} Returns a string with the id token of the user. |
||
27 | * @rejects {Object} Returns an object with errors if rejected |
||
28 | */ |
||
29 | signIn(email, password) { |
||
30 | return this.firebase.auth().signInWithEmailAndPassword(email, password) |
||
31 | .then((user) => { |
||
0 ignored issues
–
show
|
|||
32 | return this.getIdToken(); |
||
33 | }) |
||
34 | .catch((error) => { |
||
35 | throw { code: error.code, message: error.message }; |
||
36 | }); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Gets the id token from the logged in user. |
||
41 | * |
||
42 | * @promise {String} Returns a string with the id token of the user. |
||
43 | * @rejects {Object} Returns an object with errors if rejected |
||
44 | */ |
||
45 | getIdToken() { |
||
46 | return this.firebase.auth().currentUser.getIdToken(true) |
||
47 | .then((idToken) => { |
||
48 | return idToken; |
||
49 | }).catch((error) => { |
||
50 | throw { code: error.code, message: error.message }; |
||
51 | }); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Verify the id token of an user. |
||
56 | * |
||
57 | * @promise {Boolean} Returns true if successful |
||
58 | * @rejects {Object} Returns an object with errors if rejected |
||
59 | */ |
||
60 | authToken(idToken) { |
||
61 | return this.admin.auth().verifyIdToken(idToken) |
||
62 | .then((decodedToken) => { |
||
0 ignored issues
–
show
|
|||
63 | return true; |
||
64 | }).catch((error) => { |
||
65 | throw { code: error.code, message: error.message }; |
||
66 | }); |
||
67 | } |
||
68 | |||
69 | } |
||
70 | |||
71 | module.exports = FirebaseAuth; |
||
72 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.