This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web login - simple html + js version (#519)
- Loading branch information
1 parent
30961b3
commit 64e2c0e
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"projects": { | ||
"default": "web-login-startupos" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.firebase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Firestore web login for StartupOS local server | ||
|
||
### Setting | ||
Port and config: `./src/index.html` | ||
|
||
### Deploy | ||
``` | ||
firebase deploy | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"hosting": { | ||
"public": "./", | ||
"ignore": [ | ||
"firebase.json", | ||
"**/.*", | ||
"**/*.md" | ||
], | ||
"headers": [ | ||
{ | ||
"source": "/*", | ||
"headers": [ | ||
{ | ||
"key": "Cache-Control", | ||
"value": "max-age=600" | ||
} | ||
] | ||
} | ||
], | ||
"rewrites": [ | ||
{ | ||
"source": "**", | ||
"destination": "/index.html" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Log-in with Google</title> | ||
<style> | ||
.google { | ||
width: 100%; | ||
display: flex; | ||
align-items: center; | ||
flex-direction: column; | ||
} | ||
.gbutton { | ||
cursor: pointer; | ||
display: none; | ||
} | ||
button { | ||
padding: 5px 10px; | ||
} | ||
.chrome { | ||
display: none; | ||
font-family: Arial; | ||
font-size: 18px; | ||
color: #7e7e7e; | ||
margin-top: 10px; | ||
} | ||
</style> | ||
<script src="https://www.gstatic.com/firebasejs/5.8.1/firebase-app.js"></script> | ||
<script src="https://www.gstatic.com/firebasejs/5.8.1/firebase-auth.js"></script> | ||
<script> | ||
const config = { | ||
apiKey: 'AIzaSyAn-A7YXlqR2JNOvlbhkVwcnsOigmphQIw', | ||
authDomain: 'startupos-5f279.firebaseapp.com', | ||
databaseURL: 'https://startupos-5f279.firebaseio.com', | ||
projectId: 'startupos-5f279', | ||
storageBucket: 'startupos-5f279.appspot.com', | ||
messagingSenderId: '160348327132' | ||
}; | ||
const port = 7000; | ||
class App { | ||
constructor() { | ||
this.firebaseApp = firebase.initializeApp(config); | ||
this.firebaseApp.auth().onAuthStateChanged(userData => { | ||
const isLogin = !!userData; | ||
if (isLogin) { | ||
this.displayElements(false, true); | ||
} else { | ||
this.displayElements(true, false); | ||
} | ||
}); | ||
} | ||
|
||
init() { | ||
if (!this.isChrome()) { | ||
this.pleaseOpenChrome(); | ||
} | ||
} | ||
|
||
login() { | ||
const provider = new firebase.auth.GoogleAuthProvider(); | ||
this.firebaseApp.auth() | ||
.signInWithPopup(provider) | ||
.then(result => { | ||
this.getJwtToken(result.user.refreshToken); | ||
}) | ||
} | ||
|
||
logout() { | ||
this.firebaseApp.auth().signOut(); | ||
} | ||
|
||
getJwtToken(refreshToken) { | ||
this.firebaseApp.auth().currentUser.getIdToken(true) | ||
.then(jwtToken => { | ||
this.sendToServer(jwtToken, refreshToken); | ||
}); | ||
} | ||
|
||
sendToServer(jwtToken, refreshToken) { | ||
const url = `http://localhost:${port}/token`; | ||
const data = { | ||
projectId: config.projectId, | ||
apiKey: config.apiKey, | ||
jwtToken: jwtToken, | ||
refreshToken: refreshToken, | ||
}; | ||
const http = new XMLHttpRequest(); | ||
http.open('POST', url, true); | ||
http.setRequestHeader('Content-type', 'application/json'); | ||
http.send(JSON.stringify(data)); | ||
} | ||
|
||
displayElements(login, logout) { | ||
const loginElement = document.getElementById('login'); | ||
const logoutElement = document.getElementById('logout'); | ||
loginElement.style.display = login ? 'block' : 'none'; | ||
logoutElement.style.display = logout ? 'block' : 'none'; | ||
} | ||
|
||
isChrome() { | ||
return /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); | ||
} | ||
|
||
pleaseOpenChrome() { | ||
document.getElementById('chrome').style.display = 'block'; | ||
} | ||
} | ||
const app = new App(); | ||
|
||
window.onload = () => { | ||
app.init(); | ||
}; | ||
</script> | ||
</head> | ||
<body> | ||
<div class="google"> | ||
<div class="gbutton" id="login"> | ||
<button onclick="app.login()"> | ||
Log-in with google | ||
</button> | ||
</div> | ||
<div class="gbutton" id="logout"> | ||
<button onclick="app.logout()"> | ||
Log Out | ||
</button> | ||
</div> | ||
<div class="chrome" id="chrome">For best support please open Chrome</div> | ||
</div> | ||
</body> | ||
</html> |