# Solution : Implement the Serialization of a Passport User

**URL:** <https://forum.freecodecamp.org/t/solution-implement-the-serialization-of-a-passport-user/633642>\
**Category:** freeCodeCamp Contributors\
**Tags:** contributing\
**Created:** [August 29, 2023, 12:48pm UTC](https://forum.freecodecamp.org/t/solution-implement-the-serialization-of-a-passport-user/633642 "2023-08-29T12:48:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![YogeshBhosale1999](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yogeshbhosale1999/32/331445_2.png) [@YogeshBhosale1999](https://forum.freecodecamp.org/u/YogeshBhosale1999)\
**Post date:** [August 29, 2023, 12:48pm UTC](https://forum.freecodecamp.org/t/solution-implement-the-serialization-of-a-passport-user/633642/1 "2023-08-29T12:48:42Z")

</div>

You need to add a secret from a padlock icon present on the top left side. Add secret as below :

> **Summary**
>
> KEY : MONGO\_URI
> 
> ## PASSWORD : mongodb+srv://Yb:1360@freecodecamp.80rlljl.mongodb.net/ORG?retryWrites=true&w=majority
> 
> ## server.js
> 
> ```auto
> 'use strict';
> require('dotenv').config();
> const express = require('express');
> const myDB = require('./connection');
> const fccTesting = require('./freeCodeCamp/fcctesting.js');
> const session = require('express-session');
> const passport = require('passport');
> const { ObjectID } = require('mongodb');
> const LocalStrategy = require('passport-local');
> 
> const app = express();
> 
> app.set('view engine', 'pug');
> app.set('views', './views/pug');
> 
> app.use(session({
> secret: process.env.SESSION_SECRET,
> resave: true,
> saveUninitialized: true,
> cookie: { secure: false }
> }));
> 
> app.use(passport.initialize());
> app.use(passport.session());
> 
> fccTesting(app); // For fCC testing purposes
> app.use('/public', express.static(process.cwd() + '/public'));
> app.use(express.json());
> app.use(express.urlencoded({ extended: true }));
> 
> myDB(async client => {
> const myDataBase = await client.db('database').collection('users');
> 
> app.route('/').get((req, res) => {
> res.render('index', {
> title: 'Connected to Database',
> message: 'Please log in'
> });
> });
> 
> passport.use(new LocalStrategy((username, password, done) => {
> myDataBase.findOne({ username: username }, (err, user) => {
> console.log(`User ${username} attempted to log in.`);
> if (err) { return done(err); }
> if (!user) { return done(null, false); }
> if (password !== user.password) { return done(null, false); }
> return done(null, user);
> });
> }));
> 
> passport.serializeUser((user, done) => {
> done(null, user._id);
> });
>   
> passport.deserializeUser((id, done) => {
> myDataBase.findOne({ _id: new ObjectID(id) }, (err, doc) => {
> done(null, doc);
> });
> });
> 
> }).catch(e => {
> app.route('/').get((req, res) => {
> res.render('index', { title: e, message: 'Unable to connect to database' });
> });
> });
>   
> const PORT = process.env.PORT || 3000;
> app.listen(PORT, () => {
> console.log(`Listening on port ${PORT}`);
> });
> 
> ```
> 
> * * *

**Challenge:** Implement the Serialization of a Passport User

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/quality-assurance/advanced-node-and-express/implement-the-serialization-of-a-passport-user)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [August 29, 2023, 12:51pm UTC](https://forum.freecodecamp.org/t/solution-implement-the-serialization-of-a-passport-user/633642/2 "2023-08-29T12:51:22Z")

</div>

Hello there.

Thank you, for your contribution. For future contributions, please wrap your solution within :

````plaintext
[details]
```
code goes here...
```
[/details]

````

Also, provide **all** of the necessary code to pass the challenge.

Also, when you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor ( `</>` ) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](http://www.freecodecamp.com/forum/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![YogeshBhosale1999](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yogeshbhosale1999/32/331445_2.png) [@YogeshBhosale1999](https://forum.freecodecamp.org/u/YogeshBhosale1999)\
**Post date:** [August 29, 2023, 12:54pm UTC](https://forum.freecodecamp.org/t/solution-implement-the-serialization-of-a-passport-user/633642/3 "2023-08-29T12:54:19Z")

</div>

Thank you so much. I will keep in mind from next time.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [February 28, 2024, 12:55am UTC](https://forum.freecodecamp.org/t/solution-implement-the-serialization-of-a-passport-user/633642/4 "2024-02-28T00:55:02Z")

</div>

Closed on February 28, 2024, 12:55am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
