<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: A Johan</title>
    <description>The latest articles on DEV Community by A Johan (@alfonsojohan).</description>
    <link>https://dev.to/alfonsojohan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F722967%2Fe60e2a2b-0e81-4b86-b5f3-57f14cc8d5c5.jpeg</url>
      <title>DEV Community: A Johan</title>
      <link>https://dev.to/alfonsojohan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alfonsojohan"/>
    <language>en</language>
    <item>
      <title>Easy Step by Step Loopback 4 JWT Authentication with Firebase</title>
      <dc:creator>A Johan</dc:creator>
      <pubDate>Mon, 11 Oct 2021 13:47:29 +0000</pubDate>
      <link>https://dev.to/alfonsojohan/loopback-4-jwt-authentication-with-firebase-1o4</link>
      <guid>https://dev.to/alfonsojohan/loopback-4-jwt-authentication-with-firebase-1o4</guid>
      <description>&lt;p&gt;The title says it all. The team decided to use Loopback 4 for the APIs as it is easy to get something working really quick. The challenge came when we wanted to integrate the authentication mechanism with our front end which was on VueJs. Firebase was our authentication server since we only needed Social logins and nothing more. Firebase takes a lot of the pain out of getting an web app with authentication up, well done Firebase team!&lt;/p&gt;

&lt;p&gt;Back to the matter at hand. Loopback 4 documentation had sections on using JWT as well as custom authentication strategies. However, it was unclear and we were really stuck for many days on how to get it working. I would like to detail out the steps we took to get it working, much as a reference to my future self and hoping to help those in similar situations.&lt;/p&gt;

&lt;p&gt;Let's scaffold a Loopback 4 application. I use Ubuntu in WSL 2 as my primary development environment. I also use &lt;code&gt;yarn&lt;/code&gt; when the scaffold asks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ lb4 mysuperapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Answer the questions and wait for the scaffold to finish. You then need to add the &lt;code&gt;firebase-admin, @loopback/authentication and @loopback/authentication-jwt&lt;/code&gt; package to your Loopback application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd mysuperapp
$ yarn add firebase-admin @loopback/authentication @loopback/authentication-jwt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Follow the instructions at &lt;a href="https://firebase.google.com/docs/admin/setup" rel="noopener noreferrer"&gt;Add the Firebase Admin SDK to your server (google.com)&lt;/a&gt; to finish setting up the admin SDK. You will need to save the JSON file with your private key to your local machine and add it to the Loopback app. I usually save it into a folder under the app root called ./keys/ and I add this folder to my &lt;code&gt;.gitignore&lt;/code&gt; file so as to avoid checking in the secret file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbb88et9vgo5z4o15qmlf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbb88et9vgo5z4o15qmlf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next step is &lt;strong&gt;IMPORTANT&lt;/strong&gt; to ensure you get your Firebase SDK setup properly. You need to have an environment variable called &lt;code&gt;GOOGLE_APPLICATION_CREDENTIALS&lt;/code&gt; defined. The value is the path to the JSON file you downloaded from Firebase earlier. You have to ensure this environment variable is present &lt;strong&gt;every time&lt;/strong&gt; before you run you Loopback app. In Linux you would do &lt;em&gt;(replace the path and file name based on the file you downloaded earlier)&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// you can do it this way
$ export GOOGLE_APPLICATION_CREDENTIALS="./keys/my_secret_file.json"
$ yarn start
// or this way before you run the app
$ GOOGLE_APPLICATION_CREDENTIALS="./keys/my_secret_file.json" yarn start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next step is to initialize Firebase. Open &lt;code&gt;application.ts&lt;/code&gt;, import the &lt;code&gt;firebase-admin&lt;/code&gt; and &lt;code&gt;loopback-authentication&lt;/code&gt; packages in the &lt;code&gt;constructor&lt;/code&gt; . Next add the Firebase initialization steps. You will need your Firebase project Id and you can get that from the project settings in the Firebase console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// application.ts
import * as firebase from "firebase-admin";
import { AuthenticationComponent } from '@loopback/authentication';
import { JWTAuthenticationComponent, TokenServiceBindings } from '@loopback/authentication-jwt';
export class MysuperappApplication extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options: ApplicationConfig = {}) {
    super(options);
    // initialize firebase 
    firebase.initializeApp({
      credential: firebase.credential.applicationDefault(),
      projectId: 'my-firebase-project'
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then add the JWT component as shown in the Loopback documentation here &lt;a href="https://loopback.io/doc/en/lb4/Authentication-tutorial.html" rel="noopener noreferrer"&gt;How to secure your LoopBack 4 application with JWT authentication&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// application.ts - Add this at the bottom of the constructor
this.component(AuthenticationComponent);
this.component(JWTAuthenticationComponent);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above will add the Authentication and JWT component to your Loopback application. That’s it. How cool is that! The last step before actually handling the Firebase code is to tell Loopback where to go for authentication. We do that by &lt;code&gt;binding&lt;/code&gt; the &lt;code&gt;TOKEN_SERVICE&lt;/code&gt; to our class that will handle decoding the Firebase token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// application.ts - add this after adding the 2 lines above
this.bind(TokenServiceBindings.TOKEN_SERVICE).toClass(FirebaseTokenService);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point you will get an error as we have not defined the class yet. Let’s do that next. Open the terminal in your application folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysuperapp$ lb4 service
? Service type: Local service class bound to application context
? Service name: FirebaseToken
   create src/services/firebase-token.service.ts
   update src/services/index.ts
Service FirebaseToken was/were created in src/services
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import this file in &lt;code&gt;application.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as firebase from "firebase-admin";
import { FirebaseTokenService } from './services';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s setup the &lt;code&gt;FirebaseTokenService&lt;/code&gt;. We have to implement the &lt;code&gt;TokenService&lt;/code&gt; interface. Since we won’t be generating any tokens we throw an error when anyone tries to use that function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// firebase-token.service.ts
// Let's define an inline error class to that Loopback 
// can properly inform the user 
class FirebaseTokenError extends Error {
  statusCode: number
  constructor(message: string, statusCode = 403) {
    super(message)
    this.statusCode = statusCode;
  }
}
@injectable({scope: BindingScope.TRANSIENT})
export class FirebaseTokenService implements TokenService {
  constructor( ) { }
  async verifyToken (token: string): Promise&amp;lt;UserProfile&amp;gt; {
     // TODO implement the token decode and verify
  }
  async generateToken (userProfile: UserProfile): Promise&amp;lt;string&amp;gt; {
    throw new FirebaseTokenError("This service is not implemented");
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next few steps are straight forward and you can get the details by reading through the Firebase documentation. Let’s decode the token and return the &lt;code&gt;UserProfile&lt;/code&gt; expected by Loopback. First add the &lt;code&gt;firebase-admin&lt;/code&gt; library to your &lt;code&gt;FirebaseTokenService&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// firebase-token.service.ts
import * as firebaseAdmin from "firebase-admin";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next implement the function to decode the token and return the &lt;code&gt;UserProfile&lt;/code&gt;. Both of these functions should be defined in your &lt;code&gt;FirebaseTokenService&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// firebase-token.service.ts
async verifyToken (token: string): Promise&amp;lt;UserProfile&amp;gt; {
   // call the admin sdk to decode the token
    const decodedToken = await firebaseAdmin
       .auth()
       .verifyIdToken(token);
   // I cast to Record&amp;lt;string, any&amp;gt; here as I need to make 
   // some changes to the object
   let userProfile: Record&amp;lt;string, any&amp;gt; = decodedToken;
   // call function to return the UserProfile from 
   // decoded token
   return this.tokenToUserProfile(userProfile);
}
/**
 * Function to convert token to UserProfile
 */
tokenToUserProfile (token: Record&amp;lt;string, any&amp;gt;): UserProfile {
   return {
     [securityId]: token.user_id,
     email: token.email,
     name: token.name,
     picture: token.picture,
     uid: token.user_id,
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that, you now have a fully functioning integration between your Loopback application and Firebase authentication. You can view the full code at my GitHub (&lt;a href="https://github.com/alfonsojohan/loopback4-firebase" rel="noopener noreferrer"&gt;https://github.com/alfonsojohan/loopback4-firebase&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>firebase</category>
      <category>authentication</category>
      <category>loopback</category>
    </item>
  </channel>
</rss>
