{"id":26569747,"url":"https://github.com/hossein-zare/express-auth-db","last_synced_at":"2026-05-03T16:33:16.341Z","repository":{"id":57231782,"uuid":"466098724","full_name":"hossein-zare/express-auth-db","owner":"hossein-zare","description":"Authentication using cookies and database in Express.js.","archived":false,"fork":false,"pushed_at":"2022-03-26T08:15:14.000Z","size":25,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-19T22:36:42.834Z","etag":null,"topics":["auth","authentication","cookie","database","express","expressjs","login"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hossein-zare.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-04T11:33:10.000Z","updated_at":"2022-03-04T14:53:32.000Z","dependencies_parsed_at":"2022-09-13T21:00:44.674Z","dependency_job_id":null,"html_url":"https://github.com/hossein-zare/express-auth-db","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hossein-zare/express-auth-db","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hossein-zare%2Fexpress-auth-db","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hossein-zare%2Fexpress-auth-db/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hossein-zare%2Fexpress-auth-db/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hossein-zare%2Fexpress-auth-db/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hossein-zare","download_url":"https://codeload.github.com/hossein-zare/express-auth-db/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hossein-zare%2Fexpress-auth-db/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32577122,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-03T06:36:36.687Z","status":"ssl_error","status_checked_at":"2026-05-03T06:36:09.306Z","response_time":103,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","authentication","cookie","database","express","expressjs","login"],"created_at":"2025-03-22T21:23:36.886Z","updated_at":"2026-05-03T16:33:16.316Z","avatar_url":"https://github.com/hossein-zare.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Express Auth DB\n\n## Usage\n```js\nconst express = require('express');\nconst cookieParser = require('cookie-parser');\nconst crypto = require('crypto');\nconst {\n    setup,\n    authenticate,\n    login,\n    logout,\n    checkAuthenticated,\n    checkNotAuthenticated\n} = require('express-auth-db');\n\n// models\nconst Auth = require('./models/auth');\nconst User = require('./models/user');\n\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\nsetup({\n    cookieName: 'key',\n    authUserIdField: 'userId',\n    createAuth: (userId, key) =\u003e Auth.create({ userId, key, })\n    checkAuth: (key) =\u003e {\n        return Auth.findOne({\n            key,\n            expiresAt: {\n                $gt: Date.now()\n            }\n        }, {\n            userId: 1\n        });\n    },\n    deleteAuth: (key) =\u003e Auth.deleteOne({ key }),\n    getUser: (id) =\u003e User.findById(id),\n    setCookie: (res, key) =\u003e {\n        res.cookie('key', key, {\n            expires: new Date(Date.now() + 3155695200000) // 100 years\n        });\n    },\n    randomKey: () =\u003e {\n        return new Promise((resolve, reject) =\u003e {\n            crypto.randomBytes(64, (e, buf) =\u003e {\n                if (e) return reject(e);\n\n                resolve(buf.toString('hex'));\n            }\n        }));\n    },\n    redirectAuthenticated: '/profile',\n    redirectUnauthenticated: '/login',\n});\n\napp.use(cookieParser());\napp.use(authenticate);\n\napp.post('/login', checkNotAuthenticated, async (req, res) =\u003e {\n    const user = await User.findOne({ username: req.body.username });\n\n    if (user \u0026\u0026 user.checkPassword(req.body.password)) {\n        await login(user._id, res);\n\n        res.send('ok');\n    } else {\n        res.send('error');\n    }\n});\n\napp.post('/logout', checkAuthenticated, (req, res) =\u003e {\n    await logout(req, res);\n\n    res.redirect('/');\n});\n\napp.get('/check', (req, res) =\u003e {\n    if (req.isAuthenticated) {\n        res.send(':)');\n    } else {\n        res.send(':(');\n    }\n});\n\napp.get('/profile', checkAuthenticated, (req, res) =\u003e {\n    res.send(`name: ${req.user.name}`);\n});\n\napp.listen(PORT, () =\u003e {\n    console.log(`Server is running at https://localhost:${PORT}`);\n});\n```\n\n## User in views\n+ view.pug\n    \n    ```pug\n    if isAuthenticated\n        #{user.name}\n    else\n        p Please log in...\n    ```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhossein-zare%2Fexpress-auth-db","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhossein-zare%2Fexpress-auth-db","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhossein-zare%2Fexpress-auth-db/lists"}