Skip to content

Latest commit

History

History
38 lines (19 loc) 路 2.37 KB

spa-oauth.md

File metadata and controls

38 lines (19 loc) 路 2.37 KB

OAuth for SPA (single page application)

The guide of using Mixin OAuth in the SPA without exposing client_secret.

Why I wrote this

In the Mixin official documentation, We can know that the only method provided to use OAuth of Mixin is to visit https://mixin.one/oauth/authorize first. This means the user will have to leave the application first to get authenticated and be redirected to the original page.

I thought it was the only way to do that before I saw 4swap used a different method. When you're logging into 4swap, you won't leave the current page. The whole process would be completed on the same page. Which is awesome. So I want to figure it out.

How did I find the answer

4swap's website is open sourced. The auth page must be related to the keyword auth / oauth, then I found this page which contains a component called f-auth-method-modal.

This component doesn't exist in 4swap-web. So I looked for other repos and found this uikit. And there're tons of f prefixed components.

The OAuth component for mixin messenger is in here, Inside <script> -> mounted(), there's a function called authorize. I considered it's what makes it all works and opened it.

I won't explain every line of it, basically, it's all about a WebSocket connection and PKCE.

Related links

uikit/authorize.ts

uikit/oauth.js

mixin.one/authorization.js

mixin.one/oauth.js

I wrapped those two files into one. So you will only need to import the spa-oauth.js and use it like spa-oauth.vue.

Files

Javascript helper: spa-oauth.js

Vue use case: spa-oauth.vue