<?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 Sergio Hidalgo on Medium]]></title>
        <description><![CDATA[Stories by Sergio Hidalgo on Medium]]></description>
        <link>https://medium.com/@shidalgo?source=rss-2bde2beabf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*B5BAPfG0FyMtumQf.jpg</url>
            <title>Stories by Sergio Hidalgo on Medium</title>
            <link>https://medium.com/@shidalgo?source=rss-2bde2beabf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 11:20:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shidalgo/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[Implementing the authorization protocol oAuth2 using Angular, Node and two tokens (Access and…]]></title>
            <link>https://medium.com/@shidalgo/implementing-the-authorization-protocol-oauth2-using-angular-node-and-two-tokens-access-and-feb1e2e8cca6?source=rss-2bde2beabf------2</link>
            <guid isPermaLink="false">https://medium.com/p/feb1e2e8cca6</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Sergio Hidalgo]]></dc:creator>
            <pubDate>Wed, 17 Apr 2019 18:17:05 GMT</pubDate>
            <atom:updated>2019-04-17T18:17:05.004Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SL0I-Xu8YFED_xsG_rttKQ.jpeg" /></figure><h3>Implementing the authorization protocol oAuth2 using Angular, Node and two tokens (Access and Refresh) — Part 3/ 3</h3><h3>Implementing in Node</h3><p>The complete code is in the following repository: <a href="https://github.com/sergiohidalgocaceres/appInterceptorBack">https://github.com/sergiohidalgocaceres/appInterceptorBack</a></p><h3>Flow</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bkPeR4FjINxHsry8IKqYTQ.png" /></figure><h3>Creating a “Helper” for tokens</h3><p>The first thing we will create is a “helper” (a class) that allows us to generate the tokens: Access and Refresh, and also validate the lifetime and integrity of the Access Token.</p><p>To generate the Access Token we will use the following packages:</p><ul><li>jwt-simple</li><li>moment</li></ul><p>To generate the Refresh Token we will use the “rand-token” package.</p><p>We will create 3 methods: createTokens, generateAccessToken and decodeAccessToken</p><h3><strong>Method: createTokens</strong></h3><pre>import jwt = require(&quot;jwt-simple&quot;)<br>import randToken = require(&quot;rand-token&quot;)<br>import moment = require(&quot;moment&quot;)</pre><pre>const keyword = &quot;LaMej0rFormaDeSaberQueUnoSabees3nseñaraLosDemas&quot;</pre><pre>const createTokens = id =&gt; {<br>    const payload = {<br>        id,<br>        iat: moment().unix(),<br>        exp: moment().add(30, &quot;seconds&quot;).unix()<br>    }</pre><pre>    const accessToken = jwt.encode(payload, keyword)<br>    const refreshToken = randToken.uid(256)<br>    <br>    return { accessToken, refreshToken }<br>}</pre><p>The method receives a parameter: the id of the user record (as defined by me).</p><p>The Access Token consists of 3 parts separated by a point:</p><p>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9<strong>.</strong>eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ<strong>.</strong>SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c</p><p>The first part, called the header, contains the algorithm to generate the signature (third part).</p><pre>{&quot;alg&quot;:&quot;HS256&quot;,&quot;typ&quot;:&quot;JWT&quot;}</pre><p>The second part, called payload, contains customized information (role, id, etc.) and information about the token (iat: emission mark, exp: date and time of expiration).</p><pre>const payload = {<br>        id,<br>        iat: moment().unix(),<br>        exp: moment().add(30, &quot;seconds&quot;).unix()<br>    }</pre><blockquote>The personalized information stored in the payload, should not be confidential information. The payload is insecure.</blockquote><p>The third part, called the signature, encoded the header and payload. To encode, a keyword stored on the server is used, never on the client side. This part guarantees the integrity of the Access Token.</p><p>To generate the Access Token, the “encode” method is used.</p><pre>const accessToken = jwt.encode(payload, keyword)</pre><p>To generate the Refresh Token the “uid” method is used.</p><pre>const refreshToken = randToken.uid(256)</pre><p>This method returns both tokens:</p><pre>return { accessToken, refreshToken }</pre><h3>Method: generateAccessToken</h3><p>This method is very similar to the previous one. The difference is that it only generates the Access Token.</p><pre>const generateAccessToken = (id: string) =&gt; {<br>       const payload = {<br>            id,<br>            iat: moment().unix(),<br>            exp: moment().add(30, &quot;seconds&quot;).unix()<br>       }</pre><pre>       const accessToken = jwt.encode(payload, keyword)</pre><pre>       return accessToken<br>}</pre><h3>Method: decodeAccessToken</h3><pre>const decodeAccessToken = accessToken =&gt; {<br>     const promise:Promise&lt;any&gt; = new Promise((resolve, reject) =&gt; {<br>          try {<br>             const payload = jwt.decode(accessToken, keyword)<br>             resolve(payload)<br>          } catch (error) {<br>              if (error.message.toLowerCase() == &quot;token expired&quot;) {<br>                  reject({<br>                         status: 401,<br>                         message: &quot;Token has expired&quot;<br>                        })<br>              } else {<br>                 reject({<br>                        status: 500,<br>                        message: &quot;Token is invalid&quot;<br>                 })<br>              }<br>          }<br>     })</pre><pre>     return promise<br>}</pre><p>This method receives as parameter the Access Token sent through a request from Angular and returns a promise that is resolved or rejected depending on the result of the “decode” method. If the “decode” method does not give an error, the promise is fulfilled and the payload is sent. If it gives an error, the promise is rejected with a message that will depend on the type of error.</p><h3>Creating the authentication middleware</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a6d87608f46d5aab922564d2e0b1d90/href">https://medium.com/media/2a6d87608f46d5aab922564d2e0b1d90/href</a></iframe><p>This middleware checks if the “authorization” header has been sent. If it is not received, the request is rejected with an error 409.</p><p>If the header has been received, the Access Token is obtained and decoded using the “decodeAccessToken” method defined above to validate the Access Token.</p><p>If the decoding is correct, the middleware uses the “next” method to go to the next middleware.</p><p>If the decoding is incorrect, the middleware rejects the request with a 401 or 500 error depending on whether the Access Token has expired or if it has been modified.</p><h3>Creating the Users controller</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27eb73b1ab63427890565602ca350860/href">https://medium.com/media/27eb73b1ab63427890565602ca350860/href</a></iframe><h3>Method: login</h3><pre>async login(req: Request, res: Response) {<br>        const data = req.body<br>        const user = this.users.find(user =&gt; user.username == data.username &amp;&amp; user.password == data.password)</pre><pre>        if (user) {<br>             const refreshToken = user.refreshToken<br>             const accessToken = generateAccessToken(user.id)<br>             <br>             res<br>               .json({ accessToken, refreshToken })<br>        } else {<br>             res<br>               .status(404)<br>               .json({<br>                  status: 404,<br>                  message: &quot;User not found&quot;<br>               })<br>        }<br>}</pre><p>This method receives the username and password through the “body”. Verify that the username and password exist. If it exists, it extracts the Refresh Token and generates an Access Token using the user’s “id”.</p><p>If the username and password do not exist, reject the request with a 404 error.</p><h3>Method: newAccessToken</h3><pre>async newAccessToken(req: Request, res: Response) {<br>     const refreshToken = req.body.refreshToken<br>     const user = this.users.find(user =&gt; user.refreshToken == refreshToken)<br>     <br>     if (user) {<br>        const accessToken = generateAccessToken(user.id)<br>        res<br>          .json({<br>              accessToken<br>          })<br>     } else {<br>        res<br>          .status(404)<br>          .json({<br>            status: 404,<br>            message: &quot;User is not found&quot;<br>        })<br>     }<br>}</pre><p>This method receives the Refresh Token as a parameter through the “body”, then searches for the user that has that Refresh Token. If it finds it, it generates a new Access Token and sends it in response to the request.</p><p>If you cannot find it, reject the request with a 404 error.</p><h3>Creating the Users route</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4627a998e1a6358aa5669c84535e944/href">https://medium.com/media/e4627a998e1a6358aa5669c84535e944/href</a></iframe><p>I have declared 3 routes:</p><ul><li>“/” (get)</li><li>“/login” (post)</li><li>“/new-access-token” (post)</li></ul><p>Only the first route is protected by the authentication middleware defined in this same article. If a request arrives at this route but the authentication header is not attached or the Access Token is invalid, it is rejected.</p><p>The other 2 routes are handled by the “login” and “getNewAccessToken” methods explained above.</p><p>With this part in Node the implementation of the protocol orAuth2 is finished. You can modify it and adapt it to your requirements.</p><p>I hope the article is useful for someone.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=feb1e2e8cca6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing the authorization protocol oAuth2 using Angular, Node and two tokens (Access and…]]></title>
            <link>https://medium.com/@shidalgo/implementing-the-authorization-protocol-oauth2-using-angular-node-and-two-tokens-access-and-e6d24cddb660?source=rss-2bde2beabf------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6d24cddb660</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Sergio Hidalgo]]></dc:creator>
            <pubDate>Wed, 17 Apr 2019 18:16:14 GMT</pubDate>
            <atom:updated>2019-04-17T18:16:14.037Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SL0I-Xu8YFED_xsG_rttKQ.jpeg" /></figure><h3>Implementing the authorization protocol oAuth2 using Angular, Node and two tokens (Access and Refresh) — Part 2/ 3</h3><h3>Implementing in Angular</h3><p>The complete code is in the following repository: <a href="https://github.com/sergiohidalgocaceres/appInterceptorFront">https://github.com/sergiohidalgocaceres/appInterceptorFront</a></p><h3>Creating an authentication service</h3><p><strong>The “login” method</strong><br> This method will send the username (or email) and password to Node and Node will respond with the two tokens: Access and Refresh Tokens. Both will be stored in the SessionStorage.</p><ul><li>The path http://localhost:3000 is the path where the server will be running in Node.</li><li>The path http://localhost:3000/users/login is a path defined within Node to perform authentication.</li></ul><p><strong>The “getNewAccessToken” method</strong><br> This method will request a new Access Token when it expires. To do this, it will attach the Refresh Token.</p><p><strong>The “logout” method</strong><br> This method will delete everything stored in the SessionStorage. It can also be deleted by closing the browser.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/575edc4facb8fe48d53f0d46a7b26422/href">https://medium.com/media/575edc4facb8fe48d53f0d46a7b26422/href</a></iframe><h3>Creating User Service</h3><p><strong>The “getUsers” method</strong><br>This method will get the list of users (or can be any other request). The interceptor, that will be created later, will attach the Access Token.<br>We will use this method to prove that the interceptor works</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8391758c41a2384cf9e401dd17684748/href">https://medium.com/media/8391758c41a2384cf9e401dd17684748/href</a></iframe><h3>What is the interceptor doing?</h3><p>Without the interceptor, the requests would go directly to the Node server</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AGEnlXt4NnoyM5rnqRSqBA.png" /></figure><p>With the interceptor, a request is captured and cloned. The “Authorization” header is added to this cloned request and then sent to the Node server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bZAgIGvJlkFxUlfi60qObA.png" /></figure><p>The Node server can respond with a 401 error, which means that the Access Token has expired.<br>At this point there are two options:</p><ol><li>Request the user to re-enter their username and password to obtain a new Access Token.</li><li>Or that the interceptor, in background, requests a new Access Token attaching the Refresh Token.</li></ol><p>The second option is more appropriate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oFfg8u40ZgWPwgH8bywzDQ.png" /></figure><p>Node responds with a new Access Token assuming the Refresh Token is valid.<br>With the new Access Token, the interceptor makes the request again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x4yG1oNVKMbSsLW5OfzGfA.png" /></figure><p>If Node responds with a 500 error or a 409 error, the interceptor does not take any action (display a message, go to the login path, etc.) but it can be modified to do one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9bj4jAkCfNGggimhvvBmug.png" /></figure><h3>Creating an interceptor</h3><p>The interceptor is very similar to a service with the difference that implements the HttpInterceptor interface. The HttpInterceptor interface forces you to create the “intercept” method.</p><p><strong>The “intercept” method</strong><br> This method receives two parameters:</p><ul><li>“req” is the request. This could be the request generated by the “getUsers” method of the User service.</li><li>“next” allows to continue with the new generated request. This new request is a clone of the original request but the “Authorization” header attached.</li></ul><p>This method returns an observable of events related to the request</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4bf96f96c17178c13a5472b8b1caa07/href">https://medium.com/media/b4bf96f96c17178c13a5472b8b1caa07/href</a></iframe><p><strong>Step 1:</strong> Get the tokens stored in SessionStorage</p><pre>let accessToken = &quot;&quot;, refreshToken = &quot;&quot;</pre><pre>const tokens = JSON.parse(sessionStorage.getItem(&quot;tokens&quot;))                         if(tokens){<br>       accessToken = tokens.accessToken<br>       refreshToken = tokens.refreshToken<br>}</pre><pre>let clonHttp: HttpRequest&lt;any&gt;<br>clonHttp = tokens ? req.clone({ headers: req.headers.append(&quot;Authorization&quot;, `Bearer ${accessToken}`) }) : req</pre><p><strong>Step 2:</strong> Clone the request and add the “Authorization” header. Note that the word “Bearer” has been added. There are several types of authorization. For authorization with tokens the type “Bearer” is used.</p><pre>let clonHttp: HttpRequest&lt;any&gt;</pre><pre>clonHttp = tokens ? req.clone({ headers: req.headers.append(&quot;Authorization&quot;, `Bearer ${accessToken}`) }) : req</pre><p><strong>Step 3:</strong> Inject the Authentication service. This service also uses the http, it can not be injected in the common way, it would give us error.<br>To inject it we do it through another service called “injector”.</p><pre>...<br>constructor(private injector: Injector) { }</pre><pre>...<br> intercept(req: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {</pre><pre>...<br> let auth = this.injector.get(AuthenticationService);<br>...<br>}</pre><p><strong>Step 4:</strong> Sending the cloned request to the Node server is done with the “handle” method of the “next” parameter.</p><pre>return next.handle(clonHttp)</pre><p><strong>Step 5:</strong> There are two types of errors. Errors of type ErrorEvent that are our errors and errors generated by some action not allowed to the users. We will focus on the seconds.</p><p>If a 401 error arrives, we will request a new Access Token through the “getNewAccessToken” method of the authentication service.<br>Once the new Access Token is received, we clone the original request again, add the “Authorization” header and send it to the Node server.</p><pre>return auth.getNewAccessToken(refreshToken)<br>    .pipe(<br>        retry(3),<br>        mergeMap(<br>             (response: any) =&gt; {<br>                 tokens.accessToken = response.accessToken<br>                 sessionStorage.setItem(&quot;tokens&quot;, JSON.stringify(tokens))<br>                 clonHttp = req.clone({ headers: req.headers.append(&quot;Authorization&quot;, `Bearer ${response.accessToken}`) })<br>                 return next.handle(clonHttp)<br>             }<br>       )<br>    )</pre><p><strong>Step 6:</strong> If a 409 error arrives, it means that the “Authorization” header was not sent because the user did not authenticate.<br>The interceptor only executes an error action through the “throwError” operator, but it could be modified to send the user to the login route.</p><pre>return throwError(&quot;User not logged&quot;)</pre><p><strong>Step 7:</strong> If an error 500 arrives, it means that the integrity of the Access Token has been corrupted or that the Refresh Token does not exist or is not valid.<br>As in the previous step, the interceptor only executes an action through the operator “throwError” but it can be modified.</p><pre>if (error.error &amp;&amp; error.error.message) {<br>       return throwError(error.error.message)                             } else {<br>       return throwError(&quot;Check your connection&quot;)                             }</pre><p><strong>Step 8: </strong>Do not forget to register the interceptor in the main module.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0bf5fadaf09872b78dbd5426851511c2/href">https://medium.com/media/0bf5fadaf09872b78dbd5426851511c2/href</a></iframe><h3>How do you see in the browser?</h3><p>First screen</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/1*GjKyM8VAFMUfOk0RWjNdFQ.jpeg" /></figure><p>If we press the “Get Users” button without being authenticated, Node responds with an error 409</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MQ9in2oF17K9xKBOasjrPw.jpeg" /></figure><p>If we authenticate (username: user01, password: 123), Node will send the two tokens: Access and Refresh.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ARL8qrxZWf2nD6o-c5WZMQ.jpeg" /></figure><p>If we are authenticated and press the “Get Users” button, Node will return the list of users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WRkODRbDIhX9q1a0f4mfgw.jpeg" /></figure><p>If the lifetime of the Access Token has expired but we press the “Get Users” button, Node will respond with a 401 error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xmqwDRsVADcXQOafQYklYg.jpeg" /></figure><p>The interceptor receives this error 401 and immediately makes a new request to obtain a new Access Token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tsiTcIYqF5AwS3vxVwBzOA.jpeg" /></figure><p>With the new Access Token, the interceptor makes the same request again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kYHXw9IurLwBbJbO-nNA6Q.jpeg" /></figure><p>So far the implementation with Angular</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6d24cddb660" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing the authorization protocol oAuth2 using Angular, Node and two tokens (Access and…]]></title>
            <link>https://medium.com/@shidalgo/implementing-the-authorization-protocol-oauth2-using-angular-node-and-two-tokens-access-and-ec9b3072d647?source=rss-2bde2beabf------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec9b3072d647</guid>
            <category><![CDATA[oauth]]></category>
            <dc:creator><![CDATA[Sergio Hidalgo]]></dc:creator>
            <pubDate>Wed, 17 Apr 2019 18:14:56 GMT</pubDate>
            <atom:updated>2019-04-17T18:14:56.863Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SL0I-Xu8YFED_xsG_rttKQ.jpeg" /></figure><h3>Implementing the authorization protocol oAuth2 using Angular, Node and two tokens (Access and Refresh) — Part 1 / 3</h3><h3>Introduction</h3><p>My intention is to show how oAuth2 works and an option to implement it. It is not my intention that this article is a tutorial of Angular and / or Node. Whoever reads this article has to know the basics of Angular and Node.<br> I have divided the story into three parts:</p><ol><li>Introduction</li><li>Implementation in Angular</li><li>Implementation in Node</li></ol><p>I will use Typescript for both Angular and Node. The code is not to go into production as it is. I have not intentionally added things such as database connection, exception handling, models, etc. to focus as much as possible on the explanation and implementation of OAuth2, which in itself is a bit long.<br> This implementation, in development, can work with the http protocol. If it goes into production (with its respective changes and additions), the https protocol should be used.</p><h3>What are interceptors?</h3><p>The interceptors are similar to the services but implement the HttpInterceptor interface. The interceptors capture the http requests to stop them, change them or add some information to them. This article will focus on the latter: “add something” and also take some action in case of an exception. That something will be the “Authorization” header with the value of an Access Token.</p><h3>How will the communication between Angular and Node be?</h3><p><strong>Step 1:</strong> From Angular, the username (or email) and password are sent to authenticate (login)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ulm5G2-lERd5gYApdU4-HQ.png" /></figure><p><strong>Step 2:</strong> If the username and password are valid, Node sends the two called tokens: Access and Refresh. The Access Token is generated every time the user authenticates himself and each time he requests a new one. The Refresh Token is generated only once per user and stored on the server side (example: database).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tf9DcT3mBeH6rXpRfrdsXw.png" /></figure><p><strong>Step 3:</strong> Angular received the tokens and stored them in the Session Storage. At some later time a request for a list is made (of whatever they want) and the header “Authorization” is attached to it with the Access Token received because that request requires that the user be authenticated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N1-DdeixhmKS_q6NHfwRMg.png" /></figure><p><strong>Step 4:</strong> Assuming that the Access Token is valid both on expiration date and in integrity (that no one has modified it), Node responds by sending the requested list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_ROG2woI4W22H8qC4eOJXg.png" /></figure><p><strong>Step 5:</strong> Suppose that the Access Token is invalid because the expiration date was exceeded but its integrity is correct. Node sends a 401 error to report the situation to Angular.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y4EDKYhxZh3TfAUje6YoNw.png" /></figure><p><strong>Step 6:</strong> Angular requests a new Access Token by sending the received Refresh Token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z8Wv9efLYHRqa_McwlkXCA.png" /></figure><p><strong>Step 7: </strong>Node receives the request with the Refresh Token. If the latter is valid (that is, it exists), send a new Access Token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lHPpha_XJJkWFXKwhKNvlA.png" /></figure><p><strong>Step 8:</strong> Angular receives the new Access Token and returns to make the request that I can not do before because the Access Token had expired.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LBG7EGkIi6WLzCcG1Y0U6g.png" /></figure><p><strong>Step 9:</strong> Assuming two scenarios:</p><ul><li>Or that the Access Token of step 5 is invalid because someone or something modified it.</li><li>Or that the Refresh Token does not exist.</li></ul><p>Node responds with error 500. At this point Angular could simply not do anything or could send the user to the login path or could do what you want them to do.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eWRMxMJtGKWtfMFuEGyhNA.png" /></figure><p><strong>Step 10</strong>: Assuming the request is made from step 3, but the “Authorization” header is not attached, Node will respond with a 409 error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ld1iF7Lj7Cpm9fIrGTMQnA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec9b3072d647" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>