0
0
Expressframework~10 mins

Session-based auth with express-session - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the express-session middleware.

Express
const session = require('[1]');
Drag options to blanks, or click blank then click option'
Aexpress-session-middleware
Bexpress-session
Csession-express
Dexpress-session-auth
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect package names like 'express-session-auth' or 'session-express'.
Forgetting to install the package before importing.
2fill in blank
medium

Complete the code to initialize express-session middleware with a secret.

Express
app.use(session({ secret: '[1]', resave: false, saveUninitialized: true }));
Drag options to blanks, or click blank then click option'
AsecretKey
B12345
CsessionSecret
DmySecret
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers instead of strings for the secret.
Leaving the secret empty or undefined.
3fill in blank
hard

Fix the error in the code to save a username in the session after login.

Express
app.post('/login', (req, res) => {
  req.session.[1] = req.body.username;
  res.send('Logged in');
});
Drag options to blanks, or click blank then click option'
Aname
Buser
Cusername
DuserName
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent property names like 'user' or 'name' which may cause confusion.
Misspelling the property name.
4fill in blank
hard

Fill both blanks to check if a user is logged in and redirect accordingly.

Express
app.get('/dashboard', (req, res) => {
  if (!req.session.[1]) {
    res.redirect('[2]');
  } else {
    res.send('Welcome to your dashboard');
  }
});
Drag options to blanks, or click blank then click option'
Ausername
B/login
C/home
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong session property like 'user' instead of 'username'.
Redirecting to the wrong path like '/home' instead of '/login'.
5fill in blank
hard

Fill all three blanks to destroy the session on logout and redirect to home.

Express
app.post('/logout', (req, res) => {
  req.session.[1](err => {
    if (err) {
      return res.status(500).send('Error logging out');
    }
    res.[2]('[3]');
  });
});
Drag options to blanks, or click blank then click option'
Adestroy
Bredirect
C/
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'end' instead of 'destroy' to clear the session.
Using 'res.send' instead of 'res.redirect' after logout.
Redirecting to a wrong path instead of '/'.