<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ishmeet Singh on Medium]]></title>
        <description><![CDATA[Stories by Ishmeet Singh on Medium]]></description>
        <link>https://medium.com/@ishmeetsingh?source=rss-a0bfc11cdde------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kZqRq7y9kdOT65m2nSCFxg.jpeg</url>
            <title>Stories by Ishmeet Singh on Medium</title>
            <link>https://medium.com/@ishmeetsingh?source=rss-a0bfc11cdde------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 10:02:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ishmeetsingh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Google auth integration with Angular 5 and angular-oauth2-oidc]]></title>
            <link>https://medium.com/@ishmeetsingh/google-auth-integration-with-angular-5-and-angular-oauth2-oidc-ed01b997e1df?source=rss-a0bfc11cdde------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed01b997e1df</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[oauth2]]></category>
            <dc:creator><![CDATA[Ishmeet Singh]]></dc:creator>
            <pubDate>Wed, 09 May 2018 07:28:53 GMT</pubDate>
            <atom:updated>2018-05-09T07:34:30.376Z</atom:updated>
            <content:encoded><![CDATA[<p>In this blog post, we will integrate our Angular(5.X) with google authentication. The post is specific to a client side application that needs a google oAuth token and that token is used to generate a JWT from the backend service. This post will assume the following:</p><ol><li>You have a REST API to generate JWT from your backend service if you provide it with the correct oAuth token.</li><li>Your backend service handles all the User specific information, i.e. permissions, authorization, etc.</li><li>You are familiar with Angular 5, google console, rest API’s and related lingo.</li></ol><p><strong>Install the dependencies:</strong></p><p>We are using an OAUTH 2.0 client for Angular 4.3+, i.e. <a href="https://github.com/manfredsteyer/angular-oauth2-oidc">angular-oauth2-oidc</a>. So, the first step is to install this in your project:</p><pre>npm install angular-oauth2-oidc</pre><p><strong>Configure it with your app:</strong></p><p>In your <em>app.module.ts,</em> import OAuthModule:</p><pre>import { OAuthModule } from ‘angular-oauth2-oidc’;</pre><p>and initialize the module</p><pre>@NgModule({</pre><pre>  declarations: [AppComponent],</pre><pre>  imports: [OAuthModule.forRoot()],</pre><pre>  providers: [],</pre><pre>  bootstrap: [AppComponent]</pre><pre>})</pre><pre>export <em>class</em> AppModule {}</pre><p>Now, in your <em>app.component.ts, </em>initialize the OAuthService with the config for google oauth 2.0</p><pre>import { OAuthService, JwksValidationHandler } from &#39;angular-oauth2-oidc&#39;;<br>export <em>class</em> AppComponent implements <em>OnInit</em> {<br>  <em>constructor</em>(private <em>oauthService</em>: OAuthService) {<br>   this.oauthService.configure(<em>%config%</em>);<br>   this.oauthService.tokenValidationHandler = new JwksValidationHandler();<br>   this.oauthService.loadDiscoveryDocumentAndTryLogin();<br>  }<br>}</pre><p>Here <em>%config% </em>is the oAuth config. Mine looks like this:</p><pre>import { AuthConfig } from &#39;angular-oauth2-oidc&#39;;</pre><pre>authConfig: AuthConfig = {</pre><pre>  issuer: &#39;https://accounts.google.com&#39;,</pre><pre>  redirectUri: window.location.origin + &#39;/index.html&#39;,</pre><pre>  clientId: environment.googleClientId,</pre><pre>  scope: &#39;openid profile email&#39;,</pre><pre>  strictDiscoveryDocumentValidation: false</pre><pre>};</pre><p>The <strong>redirectUri</strong> would be “something.com/index.html”, is our base url since we are serving the <strong>dist/</strong> folder through <strong>nginx</strong>.</p><p>Note: This <strong>redirectUri </strong>should be added to the oAuth API in your Google cloud platform console if you have such restrictions.</p><blockquote><strong>Authorized JavaScript origins: </strong>‘your domain, for eg: localhost:4200’</blockquote><blockquote><strong>Authorized redirectUri’s</strong>: ‘localhost:4200/index.html’</blockquote><p><strong>Initiate login:</strong></p><p>Now, to initiate google login, you must import the oAuthService into your component and do the following:</p><pre>//Initiate the oauthservice just like above<br>this.oauthService.initImplicitFlow();</pre><p><strong>After succesful login:</strong></p><p>After successful login your redirected URL should look like this:</p><pre>http://localhost:4200/index.html#state=<em>%someToken%</em>&amp;token_type=Bearer&amp;expires_in=3600&amp;id_token=<em>%someToken%</em>g&amp;authuser=1&amp;hd=localhost&amp;session_state=<em>%someToken%</em>&amp;prompt=none</pre><p><strong><em>id_token </em></strong>is the one we are looking for. We are not concerned with the other parameters. Read here if you are curious about what other params are there for: <a href="https://developers.google.com/actions/identity/oauth2-code-flow">https://developers.google.com/actions/identity/oauth2-code-flow</a></p><p><strong>How to use this token:</strong></p><p>Now, you have the oAuth token in your url hash. To grab this out of there you can use this little utility:</p><pre>export <em>function</em> getParamsObjectFromHash() {</pre><pre><em>  const</em> hash = window.location.hash ? window.location.hash.split(&#39;#&#39;) : [];</pre><pre><em>  let</em> toBeReturned = {};</pre><pre>  if (hash.length &amp;&amp; hash[1].split(&#39;&amp;&#39;).length) {</pre><pre>    toBeReturned = hash[1].split(&#39;&amp;&#39;).reduce((<em>acc</em>, <em>x</em>) <em>=&gt;</em> {</pre><pre><em>      const</em> hello = x.split(&#39;=&#39;);</pre><pre>      if (hello.length === 2) acc[hello[0]] = hello[1];</pre><pre>        return acc;</pre><pre>    }, {});</pre><pre>  }</pre><pre>  return <em>Object</em>.keys(toBeReturned).length ? toBeReturned : null;</pre><pre>}</pre><p><em>This utility has one caveat, if you have the hashbang mode on, then hash array will contain one empty value or your route name. So, consider that before using this.</em></p><p>In our scenario, we grabbed this token in our route authenticator and used it to make a JWT token through an API of our backend service. <br>It is your choice now on how to proceed.</p><p><strong>To logout:</strong></p><pre>this.oauthService.logOut();</pre><p>Thanks for reading. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed01b997e1df" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>