DEV Community

Cover image for Guest Login in JavaScript 🤯

Guest Login in JavaScript 🤯

Rajesh Joshi on January 02, 2022

Whether a project is small, medium, or huge, it's most common necessity is authentication. In few cases, it is just required to not to ask user for...
Collapse
 
mcwolfmm profile image
mcwolfmm

show image code? are you serious?

to talk about login, you need to associate the user ID with a session. add another collection of session data and the article may make some sense.

and for god's sake, don't give the code a picture

Collapse
 
rajeshj3 profile image
Rajesh Joshi

As per the request, I've provided the code this time.

Collapse
 
pbatey profile image
Phil Batey

I don’t understand the use case for anonymous login.

Will the user credentials persist across sessions?

What happens if they close their browser?

Collapse
 
premierdan profile image
premierdan

Honestly, this needs further clarification.

Collapse
 
premierdan profile image
premierdan

🙏

Collapse
 
rajeshj3 profile image
Rajesh Joshi

Your thought is obvious, in normal situations Anonymous authentication is rare and there is no such requirements as well.

But, if you look into some specific kind of softwares like, Mobile Games (PUBG for example). It is really handy to have Anonymous authentication (also known as Guest Login) both for game testers or end users in general.

In those kinds of applications, the functionality to adding Google, Facebook, Twitter, etc. accounts in the future is possible, to persist the user data, which is also possible in Firebase as well.

I hope this answers your question buddy.

Thanks 😊

Collapse
 
rajeshj3 profile image
Rajesh Joshi

Will the user credentials persist across sessions?

Yes, the session will persist untill you remove the user data from indexedDB or the session expires.

What happens if they close their browser?

The session will persist even after you restart your system itself.

Collapse
 
cbretus profile image
cb-retus

I see the authentication token is in the code, of course, but is this part exposed in javascript aka in the browser?

if that's the case, I wouldn't recommend this approach.

I worked 6½ year in an online gambling company and we knew that client-side information such as authentication tokens is a no-go, even for guest-logins.

You could make a "Session" service and let it produce a session token and then send that to the guest, then each authentication token would be unique + you need a server side way of expiring these tokens too.

I hope and assume you use Firebase for that purpose?

Collapse
 
ats1999 profile image
Rahul kumar

You can embed the cide

Collapse
 
rajeshj3 profile image
Rajesh Joshi

Hey, as per your request, I've provided the code this time.

Collapse
 
daasrattale profile image
Elattar Saad

Hi fella,
The article is good, such I have some point you may want to know:
Please, don't put screenshots of code it's really hard for people to try your example (or you may want to associate this article article with GitHub repo).
Don't mind to the toxicity of some comments, but we need a clearer use case for the anonymous auth (why should we use it).
Please feel free to contact me is you need any kind of guidance.
My regards.

Collapse
 
rajeshj3 profile image
Rajesh Joshi

As per the request, I've provided the code.

As you asked, "why should we use it"

There are lot of case scenerios where we needs Guest Login. This article helps you out in implementing that case.

Collapse
 
dmitryame profile image
Info Comment hidden by post author - thread only accessible via permalink
Dmitry Amelchenko

What is the purpose of creating authentication in the first place?
Is it necessary to allow the centralized server to know who you are?
What if, instead, the users would notify only their friends, who they actively interact with, about their identity, leaving the information stored in the centralized server completely de-anonymized?

Read more about this approach at following link:
dev.to/dmitryame/p4p-peer-4-peer-i...

Some comments have been hidden by the post's author - find out more