I've been looking for resources on how to implement user auth in an Electron App.
I'd like to utilize third party services like Github to allow users to login and signup. With a "regular" Node.js application, I would likely utilize something like passport.js or similar to implement this.
My confusion arises because Electron apps are client side, so having things like your client secret keys in client side code seems wrong. So what is the process of implementing 3rd party user auth in Electron apps?
Think of electron app as a standard browser page. Then you will have standard oauth2 flow.
First of all you need middleware server where you will store clientId and clientSecret for third party services.
You need to create something like session between electron app and middleware server (below I will show example).
Below I will show example process to authorize github.
You need use https.
Lets assume that your middleware server is available on example.com. Your need minimum two endpoints :
Github client_id and client_secret are stored only in this server.
Your electron app send GET request to https://example.com/initAuth/
.
And your server should generate two uuid's. Which should be stored as pair (for example in redis). One uuid is for state
parameter in authorization github link and second one as simple session/token to identify your electron app.
Your server should build url to github access github access :
GET https://github.com/login/oauth/authorize
where
redirect_uri
will be your second endpoint - https://example.com/oauth/token
state
will be your first uuidNow you return from this endpoint to electron session/token uuid and built url.
Your electron show link with target="_blank" - it should be opened in separe tab/window. Electron should remember session/token uuid.
When user click link he will get into oauth process where he accept your app. And then he will be redirected to your middleware server second endpoint (https://example.com/oauth/token)
Your server will get in this endpoint code
and state
. YOur server should check if it has registered electron app with this state
. And if it exist then server need exchange code
and client_secret
for access_token
(I will not explain it - this is standard oauth flow).
Now store it in temporary storage (redis) access_token and both uuid's.
And as response render html view with script which will close this tab or just normal html view with some message.
Your electron app need to know if middleware server has access_token.
Or alternativly you can store access_token in your middleware server and your electron wont send requests to github but only to your server and your server will send requests to github and responses return to electron.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With