<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/twitter-login-using-node-and-mysql-broysf37w" -->

---
title: Twitter Login Using Node and MySQL | daily.dev
description: A step-by-step rebuild of a Twitter (X) login integration using Node.js, Express 5, Passport 0.7 with passport-twitter, and mysql2. Covers registering an X...
canonical: https://daily.dev/posts/twitter-login-using-node-and-mysql-broysf37w
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Twitter Login Using Node and MySQL | daily.dev
og:description: A step-by-step rebuild of a Twitter (X) login integration using Node.js, Express 5, Passport 0.7 with passport-twitter, and mysql2. Covers registering an X...
og:url: https://daily.dev/posts/twitter-login-using-node-and-mysql-broysf37w
og:image: https://api.daily.dev/og/posts/BRoYsf37w.png
og:image:alt: Twitter Login Using Node and MySQL
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Twitter Login Using Node and MySQL

**[Planet MySQL](https://daily.dev/sources/planet-mysql)** · 10 min read · 0 upvotes · 0 comments

## Summary

A step-by-step rebuild of a Twitter (X) login integration using Node.js, Express 5, Passport 0.7 with passport-twitter, and mysql2. Covers registering an X developer app, choosing between OAuth 1.0a and OAuth 2.0 with PKCE, setting up sessions, writing an upsert query to avoid duplicate-key crashes on repeat logins, and fixing three breaking changes from the original tutorial: logout now requires a callback, the session cookie option changed from key to name, and callback URLs must match exactly. Includes a config flag to make MySQL storage optional and an FAQ section addressing common errors.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://codeforgeek.com/twitter-login-using-node>

## Questions this post answers

### Why does Passport logout throw an error saying done is not a function in my Node app?

Current Passport versions (0.7 and later) end sessions asynchronously, so calling req.logout() without a callback throws an error naming the missing function. Passing a callback function to req.logout(), which forwards any error to Express via next(err), fixes the issue and is required for Passport 0.7 to work correctly.

_Developers hitting Passport breaking changes track fixes like this one on daily.dev._

### What data type should I use for a Twitter or X user id primary key in MySQL?

Use BIGINT UNSIGNED rather than a regular INT, because X's snowflake-style numeric user ids exceed the range of a 32-bit integer. Older tutorials used INT with a display width in parentheses, but display widths are deprecated and have no effect on actual storage capacity, so they don't solve the overflow problem.

_daily.dev helps developers designing schemas around third-party API ids avoid overflow pitfalls._

### How do I avoid duplicate key errors when a user logs in twice with Passport and MySQL?

Use an INSERT ... ON DUPLICATE KEY UPDATE (upsert) statement instead of a plain insert inside the Passport verify function, keyed on the user's numeric id. This inserts a new row for first-time users and refreshes the username on repeat logins, tested against MariaDB 10.11 with mysql2 3.24.3 across two consecutive logins without crashing.

_daily.dev keeps developers wiring OAuth logins to a database current on patterns like upserts._

## Similar posts on daily.dev

- [NodeJS MySQL Create Connection](https://daily.dev/posts/nodejs-mysql-create-connection-9vbhox1al) · Planet MySQL · 0 upvotes · 0 comments
- [A Practical Guide to Secure Authentication for Developers](https://daily.dev/posts/a-practical-guide-to-secure-authentication-for-developers-p5rufwxgq) · SitePoint · 0 upvotes · 0 comments
- [How To Use JSON Web Tokens \(JWTs\) in Express.js](https://daily.dev/posts/how-to-use-json-web-tokens-jwts-in-express-js-72ojkkino) · DigitalOcean Community · 7 upvotes · 0 comments
- [How OAuth works: GitHub and Google login explained](https://daily.dev/posts/how-oauth-works-github-and-google-login-explained-z7bhexnib) · Flavio Copes · 1 upvotes · 0 comments

---

Tags: [#nodejs](https://daily.dev/tags/nodejs), [#mysql](https://daily.dev/tags/mysql), [#oauth](https://daily.dev/tags/oauth), [#express](https://daily.dev/tags/express)

[View this post on daily.dev](https://daily.dev/posts/twitter-login-using-node-and-mysql-broysf37w)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Twitter Login Using Node and MySQL","url":"https://daily.dev/posts/twitter-login-using-node-and-mysql-broysf37w","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/twitter-login-using-node-and-mysql-broysf37w"},"datePublished":"2026-09-05T09:54:24.131Z","dateModified":"2026-09-05T11:01:11.618Z","description":"A step-by-step rebuild of a Twitter (X) login integration using Node.js, Express 5, Passport 0.7 with passport-twitter, and mysql2. Covers registering an X...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/3492284ce7155c0f61c5c2f99678915a?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/3492284ce7155c0f61c5c2f99678915a?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Planet MySQL","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet MySQL","logo":"https://media.daily.dev/image/upload/s--UKAtmXTq--/f_auto,q_auto/v1780213421/logos/planet-mysql?_a=BAMAMiWQ0","url":"https://daily.dev/sources/planet-mysql"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/twitter-login-using-node-and-mysql-broysf37w","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"nodejs,mysql,oauth,express","timeRequired":"PT10M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet MySQL","item":"https://daily.dev/sources/planet-mysql"},{"@type":"ListItem","position":3,"name":"Twitter Login Using Node and MySQL"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/twitter-login-using-node-and-mysql-broysf37w#faq","mainEntity":[{"@type":"Question","name":"Why does Passport logout throw an error saying done is not a function in my Node app?","acceptedAnswer":{"@type":"Answer","text":"Current Passport versions (0.7 and later) end sessions asynchronously, so calling req.logout() without a callback throws an error naming the missing function. Passing a callback function to req.logout(), which forwards any error to Express via next(err), fixes the issue and is required for Passport 0.7 to work correctly. Developers hitting Passport breaking changes track fixes like this one on daily.dev."}},{"@type":"Question","name":"What data type should I use for a Twitter or X user id primary key in MySQL?","acceptedAnswer":{"@type":"Answer","text":"Use BIGINT UNSIGNED rather than a regular INT, because X's snowflake-style numeric user ids exceed the range of a 32-bit integer. Older tutorials used INT with a display width in parentheses, but display widths are deprecated and have no effect on actual storage capacity, so they don't solve the overflow problem. daily.dev helps developers designing schemas around third-party API ids avoid overflow pitfalls."}},{"@type":"Question","name":"How do I avoid duplicate key errors when a user logs in twice with Passport and MySQL?","acceptedAnswer":{"@type":"Answer","text":"Use an INSERT ... ON DUPLICATE KEY UPDATE (upsert) statement instead of a plain insert inside the Passport verify function, keyed on the user's numeric id. This inserts a new row for first-time users and refreshes the username on repeat logins, tested against MariaDB 10.11 with mysql2 3.24.3 across two consecutive logins without crashing. daily.dev keeps developers wiring OAuth logins to a database current on patterns like upserts."}}]}
```

