repl.it
@Coder100/

Repl.it Authentication

Nodejs

It comes back with a game!

fork
loading
Files
  • index.js
  • temp
  • config.json
  • jsonstore.io.js
  • new.html
  • Packager files
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
 * Hello! Welcome to the new and improved version of repl.it 
 * authentication with express!
 * 
 * In this edition, 
 *  * We will now learn to make accounts with jsonstore.io
 *  * Learn how to use mustache.io
 *      Which is way easier in my opinion
 *      https://mustache.github.io/mustache.5.html
 *  * Verify there are only 3 repl.it account headers
 *      Just uncomment line 57
 *  * Cleaner code and more commenting!
 *  * body-parser might have a use!
 *      Read on in the code to find out!
 * 
 * Enjoy!
 */
/// Dependencies for server */
//  Initiate App
//  ↓↓↓
const app = require('express')();
//        ↑↑↑↑↑↑↑
//        Express
// body-parser
const bodyParser = require('body-parser');
// jsonstore
const jsonstore = require("./jsonstore.io.js");
// Body parser (useless for now)
app.use(bodyParser.urlencoded({ extended: true }));
// DataBase
// |- To make a jsonstore, go to jsonstore.io and then
// |- Make a .env file and paste in the key like the template
// |- below (remember no spaces):
// KEY=[key]
const store = new jsonstore(process.env.KEY);

// Use this to clear your database if necessary
// (like corrupted data comes in)
// store.delete();

/// Dependecies for templating */
const mustacheExpress = require('mustache-express');
// Now all mustache files will be parsed as long as their
// Extension is .htm! Part 1: Set up for mustache-express
app.engine('htm', mustacheExpress());
// Set where the files will come from.
app.set('views', './temp');
// Extension is .htm! Part 2: Set up for express
app.set('view engine', 'htm');

/// Get home page (/) */
app.get('/', (req, res) => {
  // Check if user has visited website or not.
  // ↓ ↓ ↓
  // Uncomment below to prove that there are only 3 repl.it headers!
  // ↓ ↓ ↓
  // console.log(JSON.stringify(req.headers));
	if (req.get('X-Replit-User-Id')) {
    // Templating time!
		res.render('welcome', {
      userid: req.get('X-Replit-User-Id'),
      username: req.get('X-Replit-User-Name'),
      userroles: req.get('X-Replit-User-Roles')
    });
    // Let's setup DataBase!
    // Remember the usernames are all UNIQUE.
    // The template for usernames is "username-id"
    let username = req.get('X-Replit-User-Name');
    let id = req.get('X-Replit-User-Id');
    // Let's make an easy click to win game!
    store.read(username+"-"+id, data => {
      // Let's check if the user has registered or not.
      if(data.data){ // If user exists
        // Do nothing
        return false;
      }else{
        // Let's set up the user if he hasn't!
        store.write(username+"-"+id, {
          score: 0
        });
      }
    });
	} else {
    // Log-in prompt.
		res.sendFile(__dirname+"/new.html");
	}
});

/// Post /score.give.to.me.kthx (the place to post your score) */
app.post("/score.give.to.me.kthx", (req, res) => {
  // just kidding! We will need body-parser for the post request!
  // Get the score
  let score = req.body.score;
  // Get secret user data!
  let username = req.body.username;
  // ↓ ↓ ↓
  // Uncomment below to prove that the headers
  // ARE PERSISTENT!
  // ↓ ↓ ↓
  // console.log(JSON.stringify(req.headers));
  store.read(username, data => {
    // Create a copy of the data
    let newdata = data.data;
    // Increase score!
    newdata.score += parseInt(score);
    // Replace old data with new data!
    store.write(username, newdata);
    // We can't forget to give visual output! lol
    res.send('Success! <a style = "color:blue;" href = "/">Click here to go back</a>!');
  });
});

/// Get /view.my.score.kthx (the place to view your score) */
app.get("/view.my.score.kthx", (req, res)=>{
  // Since we proved the headers were persistent (yay)
  // We can find the user ourselves!
  let username = req.get('X-Replit-User-Name');
  let id = req.get('X-Replit-User-Id');
  let user = username+"-"+id;
  // Read data
  store.read(user, data => {
    // Send data
    res.send('Your <em>hard earned</em> score is '+data.data.score+'! Yay! <a style = "color:blue;" href = "/">Click here to go back</a>!');
  });

});

/// Listening */
app.listen(8080);


/** Anyone else tired of commenting? **/
Fetching token
?