{"id":13632539,"url":"https://github.com/mxstbr/passport-magic-login","last_synced_at":"2025-04-12T23:29:31.405Z","repository":{"id":39861276,"uuid":"328175533","full_name":"mxstbr/passport-magic-login","owner":"mxstbr","description":"Passwordless authentication with magic links for Passport.js.","archived":false,"fork":false,"pushed_at":"2024-07-30T08:52:08.000Z","size":416,"stargazers_count":669,"open_issues_count":18,"forks_count":44,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-10T00:41:25.173Z","etag":null,"topics":["authentication","express","magiclink","passport"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/mxstbr.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-09T14:39:28.000Z","updated_at":"2025-04-04T00:55:39.000Z","dependencies_parsed_at":"2024-11-30T01:03:19.359Z","dependency_job_id":"ea7924a2-e01a-4450-8ba5-2a122ee71d11","html_url":"https://github.com/mxstbr/passport-magic-login","commit_stats":{"total_commits":62,"total_committers":10,"mean_commits":6.2,"dds":"0.22580645161290325","last_synced_commit":"5db25a5a408d19d2e05efa6e5c98f7ca7bc7932c"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mxstbr%2Fpassport-magic-login","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mxstbr%2Fpassport-magic-login/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mxstbr%2Fpassport-magic-login/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mxstbr%2Fpassport-magic-login/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mxstbr","download_url":"https://codeload.github.com/mxstbr/passport-magic-login/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248646892,"owners_count":21139079,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["authentication","express","magiclink","passport"],"created_at":"2024-08-01T22:03:06.258Z","updated_at":"2025-04-12T23:29:31.377Z","avatar_url":"https://github.com/mxstbr.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"![passport-magic-login](https://user-images.githubusercontent.com/7525670/104158644-0c61f400-53ee-11eb-960f-167c6ebd3ec9.png)\n\nPasswordless authentication with magic links for Passport.js 🔑\n\n- User signup and login without passwords\n- Supports magic links sent via email, SMS or any other method you prefer\n- User interface agnostic: all you need is an input and a confirmation screen\n- Handles secure token generation, expiration and confirmation\n\nOriginally implemented by [Tobias Lins](https://twitter.com/linstobias) for [Splitbee](https://splitbee.io) and eventually extracted for [Feedback Fish](https://feedback.fish):\n\n\u003cdiv align=\"left\"\u003e\n\n\u003cimg width=\"32%\" alt=\"Screenshot 2021-01-09 at 16 55 23\" src=\"https://user-images.githubusercontent.com/7525670/104096256-ae24fc00-529b-11eb-9d21-cebae7bc706d.png\"\u003e\n\n\u003cimg width=\"32%\" alt=\"Screenshot 2021-01-09 at 16 55 28\" src=\"https://user-images.githubusercontent.com/7525670/104096254-ad8c6580-529b-11eb-9c96-d12e9d14c543.png\"\u003e\n\n\u003cimg width=\"32%\" alt=\"Screenshot 2021-01-09 at 16 56 24\" src=\"https://user-images.githubusercontent.com/7525670/104096252-a9604800-529b-11eb-92d5-31a144871fe4.png\"\u003e\n\n\u003c/div\u003e\n\n## Usage\n\nTo use magic link authentication, you have to:\n\n1. Setup the Passport strategy and Express routes on your server\n2. POST a request with the users email or phone number from the client once they have entered it into the login input\n\n### Installation\n\n```\nnpm install passport-magic-login\n```\n\n### Frontend usage\n\nThis is what the usage from the frontend looks like once you've set it all up. It only requires a single request:\n\n```JS\n// POST a request with the users email or phone number to the server\nfetch(`/auth/magiclogin`, {\n  method: `POST`,\n  body: JSON.stringify({\n    // `destination` is required.\n    destination: email,\n    // However, you can POST anything in your payload and it will show up in your verify() method\n    name: name,\n  }),\n  headers: { 'Content-Type': 'application/json' }\n})\n  .then(res =\u003e res.json())\n  .then(json =\u003e {\n    if (json.success) {\n      // The request successfully completed and the email to the user with the\n      // magic login link was sent!\n      // You can now prompt the user to click on the link in their email\n      // We recommend you display json.code in the UI (!) so the user can verify\n      // that they're clicking on the link for their _current_ login attempt\n      document.body.innerText = json.code\n    }\n  })\n```\n\n### Backend setup\n\nTo make this work so easily, you first need to setup passport-magic-login:\n\n```JS\nimport MagicLoginStrategy from \"passport-magic-login\"\n\n// IMPORTANT: ALL OPTIONS ARE REQUIRED!\nconst magicLogin = new MagicLoginStrategy({\n  // Used to encrypt the authentication token. Needs to be long, unique and (duh) secret.\n  secret: process.env.MAGIC_LINK_SECRET,\n\n  // The authentication callback URL\n  callbackUrl: \"/auth/magiclogin/callback\",\n\n  // Called with th e generated magic link so you can send it to the user\n  // \"destination\" is what you POST-ed from the client\n  // \"href\" is your confirmUrl with the confirmation token,\n  // for example \"/auth/magiclogin/confirm?token=\u003clongtoken\u003e\"\n  sendMagicLink: async (destination, href) =\u003e {\n    await sendEmail({\n      to: destination,\n      body: `Click this link to finish logging in: https://yourcompany.com${href}`\n    })\n  },\n\n  // Once the user clicks on the magic link and verifies their login attempt,\n  // you have to match their email to a user record in the database.\n  // If it doesn't exist yet they are trying to sign up so you have to create a new one.\n  // \"payload\" contains { \"destination\": \"email\" }\n  // In standard passport fashion, call callback with the error as the first argument (if there was one)\n  // and the user data as the second argument!\n  verify: (payload, callback) =\u003e {\n    // Get or create a user with the provided email from the database\n    getOrCreateUserWithEmail(payload.destination)\n      .then(user =\u003e {\n        callback(null, user)\n      })\n      .catch(err =\u003e {\n        callback(err)\n      })\n  }\n  \n  \n  // Optional: options passed to the jwt.sign call (https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback)\n  jwtOptions: {\n    expiresIn: \"2 days\",\n  }\n})\n\n// Add the passport-magic-login strategy to Passport\npassport.use(magicLogin)\n```\n\nOnce you've got that, you'll then need to add a couple of routes to your Express server:\n\n```JS\n// This is where we POST to from the frontend\napp.post(\"/auth/magiclogin\", magicLogin.send);\n\n// The standard passport callback setup\napp.get(magicLogin.callbackUrl, passport.authenticate(\"magiclogin\"));\n```\n\nThat's it, you're ready to authenticate! 🎉\n\n## License\n\nLicensed under the MIT license. See [LICENSE](./LICENSE) for more information!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmxstbr%2Fpassport-magic-login","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmxstbr%2Fpassport-magic-login","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmxstbr%2Fpassport-magic-login/lists"}