<?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: Young Suk Ahn Park</title>
    <description>The latest articles on DEV Community by Young Suk Ahn Park (@ysahnpark).</description>
    <link>https://dev.to/ysahnpark</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%2F564010%2Fe9d90148-33e0-4f70-ad80-7099f71d0d02.jpeg</url>
      <title>DEV Community: Young Suk Ahn Park</title>
      <link>https://dev.to/ysahnpark</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ysahnpark"/>
    <language>en</language>
    <item>
      <title>Ionic React App with Social Login</title>
      <dc:creator>Young Suk Ahn Park</dc:creator>
      <pubDate>Thu, 21 Jan 2021 04:15:06 +0000</pubDate>
      <link>https://dev.to/ysahnpark/ionic-react-app-with-social-login-40a6</link>
      <guid>https://dev.to/ysahnpark/ionic-react-app-with-social-login-40a6</guid>
      <description>&lt;p&gt;TL;DR, This article is a quick PoC on integrating Ionic React App with Social Login (Google/Facebook) using OAuth2+PKCE. &lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Developing (learning to develop) a mobile app was on my list for several years. I was too lazy to learn a new language (Objective C) and Android Java development seemed a little convoluted. Then came the hybrid model (basically Cordova), and  new language or iOS (Swift), and different other languages and frameworks: Xamarin, NativeScript, Flutter, ReactNative, Ionic.&lt;/p&gt;

&lt;p&gt;Now that Ionic supports React and has a novel way of deploying on mobile - Capacitor. I finally decided to give it a try.&lt;/p&gt;

&lt;p&gt;My first order concern was Social Login. If I am to build an app, I want to lower the barrier of entry by allowing them to login with popular social platforms.&lt;/p&gt;

&lt;p&gt;I have built web applications that used social login using OpenID Connect (OIDC) specification, using the OAuth 2.0 Authorization Code grant type. But I was not sure how that would work in a mobile environment using the hybrid model, which is basically a web app running locally on the device.&lt;/p&gt;

&lt;p&gt;So this is my write up on the Ionic React Google Login Proof of Concept (PoC) based on a demo repo I found in GitHub.&lt;/p&gt;

&lt;h2&gt;
  
  
  Researching the Auth Libraries
&lt;/h2&gt;

&lt;p&gt;I did some research and found few libraries that can help me integrate Auth in Ionic app built with Capacitor:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/wi3land/ionic-appauth" rel="noopener noreferrer"&gt;ionic-appauth&lt;/a&gt;: 35 GitHub stars. &lt;br&gt;
This repo does not have version tags. Ionic-AppAuth is an implementation of the openid’s &lt;a href="https://github.com/openid/AppAuth-JS" rel="noopener noreferrer"&gt;AppAuth-JS&lt;/a&gt; for Ionic development. The documentation is minimal (close to none), but  has a React sample that you can examine:  &lt;a href="https://github.com/wi3land/ionic-appauth-react-demo" rel="noopener noreferrer"&gt;https://github.com/wi3land/ionic-appauth-react-demo&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/chemerisuk/cordova-plugin-firebase-authentication" rel="noopener noreferrer"&gt;cordova-plugin-firebase-authentication&lt;/a&gt;: 79 GitHub stars.&lt;br&gt;
This is the project that is listed in the &lt;a href="https://ionicframework.com/docs/native/firebase-authentication" rel="noopener noreferrer"&gt;ionicframework.com&lt;/a&gt; site. Even though it has ‘cordoba’ in its name, it claims to be compatible with Capacitor. It uses Firebase as the auth server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/baumblatt/capacitor-firebase-auth" rel="noopener noreferrer"&gt;capacitor-firebase-auth&lt;/a&gt;: 164 GiHub stars.&lt;br&gt;
Another library that uses Firebase as the Auth server.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The other alternative is to use Ionic’s &lt;a href="https://ionic.io/docs/auth-connect" rel="noopener noreferrer"&gt;Auth Connect&lt;/a&gt; which requires you to purchase the license.&lt;/p&gt;

&lt;p&gt;I ultimately decided to go for #1 - to use the &lt;code&gt;ionic-appauth&lt;/code&gt; library. It is open source (no license required), and is based on standard using Oauth2 Authorization Code Grant with Proof Key for Code Exchange (PKCE), which means you have a wider option of choosing the auth server providers.&lt;/p&gt;

&lt;p&gt;The rest of the article gives a quick instruction on how to configure and run the &lt;code&gt;ionic-appauth&lt;/code&gt; Demo App with Okta and Google Login.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://scotch.io/tutorials/add-social-login-to-ionic-apps" rel="noopener noreferrer"&gt;This article&lt;/a&gt; by Matt Raible was key input to my PoC.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;ionic-appauth&lt;/code&gt; Demo App
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Setting Up the Auth Server &amp;amp; Configuring the App
&lt;/h3&gt;

&lt;p&gt;As I previously mentioned, React is my framework of choice. Thankfully I found a &lt;a href="https://github.com/wi3land/ionic-appauth-react-demo" rel="noopener noreferrer"&gt;Ionic React AppAuh demo app&lt;/a&gt; in GitHub which facilitated my PoC.&lt;/p&gt;

&lt;p&gt;Sadly the demo doesn’t come with any documentation, not even README, so I had to analyze the code. Fortunately the codebase is simple. I found that the auth related configurations are set in the src/services/AuthService.ts file.&lt;/p&gt;

&lt;p&gt;I just needed to modify the values in the authConfig:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;authService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;client_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;appauth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// AHA! These are the values I need to change&lt;/span&gt;
      &lt;span class="na"&gt;server_host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:5200&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;redirect_url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;isPlatform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;capacitor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;com.appauth.demo://callback&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/loginredirect&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;end_session_redirect_url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;isPlatform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;capacitor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt;  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;com.appauth.demo://endSession&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/endredirect&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;scopes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;openid offline_access&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;pkce&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I used Okta as the Auth Server. The free developer account was sufficient to run the Demo. You can create one at &lt;a href="https://developer.okta.com" rel="noopener noreferrer"&gt;https://developer.okta.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A quick summary of setting up Okta is as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to "Application" and click the "Add Application" button.&lt;/li&gt;
&lt;li&gt;Select Single-Page App and fill in the form. The most important fields are:

&lt;ul&gt;
&lt;li&gt;In the Login Redirect URI enter: &lt;a href="http://localhost:8100/loginredirect" rel="noopener noreferrer"&gt;http://localhost:8100/loginredirect&lt;/a&gt; and com.appauth.demo://callback&lt;/li&gt;
&lt;li&gt;In the Login Redirect URI enter: &lt;a href="http://localhost:8100/endredirect" rel="noopener noreferrer"&gt;http://localhost:8100/endredirect&lt;/a&gt; and com.appauth.demo://endSession
The first uri is for web, the second is for the mobile native (e.g. capacitor)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since the title of this article is about Social Login, we will add federated Google login.&lt;br&gt;
In Okta, &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to “Users” -&amp;gt; “Social &amp;amp; Identity Providers”&lt;/li&gt;
&lt;li&gt;Click on Add Identity Provide and select "Google"&lt;/li&gt;
&lt;li&gt;Give the IdP a name and copy paste the Client ID and Client Secret from &lt;a href="https://console.developers.google.com/" rel="noopener noreferrer"&gt;Google Dev Console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The scopes should be &lt;code&gt;email, openid, profile&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Once the Google IdP is successfully added, you will see an entry in the list. Click the dark circle icon to the left of the name&lt;/li&gt;
&lt;li&gt;You will need the IdP ID for configuring your Ionic application.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now in the code, modify the config in &lt;code&gt;src/services/AuthService.ts&lt;/code&gt; replacing  the &lt;code&gt;client_id&lt;/code&gt; and &lt;code&gt;server_host&lt;/code&gt; accordingly. FYI, the &lt;code&gt;server_host&lt;/code&gt; is &lt;code&gt;https://{okta domain}&lt;/code&gt; e.g.:  &lt;a href="https://dev-XYZ.okta.com" rel="noopener noreferrer"&gt;https://dev-XYZ.okta.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, for the demo app to connect with Google IdP through Okta, you must send extra parameters in the authService.signIn() method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleSignIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="nx"&gt;Auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;signIn&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;idp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-idp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Initially I used Auth0 as my Auth server but learned that the OIDC well-known endpoint returns metadata with missing &lt;code&gt;end_session_endpoint&lt;/code&gt; attribute. This caused the app to fail when I clicked the “Sign Out” Button.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Running the App
&lt;/h3&gt;

&lt;p&gt;Now that it is properly configured you can run the application locally as web app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ionic serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command will open the browser tab and load the app. I suggest opening the page in an incognito tab to avoid the cache interfering with full login experience. &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%2Fi%2Ff1zyqyodmuhhslb0pu8z.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%2Fi%2Ff1zyqyodmuhhslb0pu8z.png" alt="web-ionic-1-landing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And when you click on "Sign In" button, it will take you to the Google Login form:&lt;br&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%2Fi%2F7l4xxiod44mfcd3qoc9g.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%2Fi%2F7l4xxiod44mfcd3qoc9g.png" alt="web-ionic-2-userid"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once successfully logged in, you should be able to get the user details:&lt;br&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%2Fi%2Fvj6i5l3svniuve67qryy.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%2Fi%2Fvj6i5l3svniuve67qryy.png" alt="web-ionic-5-user-details"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Running the Mobile App
&lt;/h3&gt;

&lt;p&gt;We are not done yet. Now we are going to proceed to deploy to mobile using Capacitor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Build the code (each time original source changes)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;ionic build

&lt;span class="c"&gt;# Copy the asset to the android project (after each ionic build)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;ionic cap copy 

&lt;span class="c"&gt;# Synchronize project by copying changes to android platform (When new plugins were added)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;ionic cap &lt;span class="nb"&gt;sync&lt;/span&gt;

&lt;span class="c"&gt;# Open the android studio&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;ionic cap open android
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assuming you have Android Studio, the last command will open the Android Studio(AS), an IDE based on IntelliJ.&lt;br&gt;
From AS, select the preferred device (simulator) - any Pixel should be fine - and press the play button.&lt;br&gt;
Once the device simulator shows up, press the "on" button to turn it on. Wait about a minute until the app is loaded on the screen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Here I have demonstrated that it is possible to integrate Ionic React mobile app with social login - in this case Google.&lt;/p&gt;

&lt;p&gt;The demo repo &lt;code&gt;https://github.com/wi3land/ionic-appauth-react-demo&lt;/code&gt; uses an older version of &lt;code&gt;ionic-appauth&lt;/code&gt;, new never version has few braking changes in the API.&lt;/p&gt;

&lt;p&gt;I have forked the project and made some minor changes including upgrading the &lt;code&gt;ionic-appauth&lt;/code&gt; version to the latest and externalizing the configuration to &lt;code&gt;.env&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;You can check my forked project with the improvements: &lt;a href="https://github.com/creasoft-dev/ionic-appauth-react-demo" rel="noopener noreferrer"&gt;https://github.com/creasoft-dev/ionic-appauth-react-demo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now that I have completed the PoC on authentication, my next step is to start building my real app.&lt;/p&gt;

&lt;p&gt;I will post my learnings on building a mobile app using Ionic React + Redux + Redux Toolkit.&lt;/p&gt;

</description>
      <category>ionic</category>
      <category>react</category>
      <category>authentication</category>
      <category>oidc</category>
    </item>
  </channel>
</rss>
