Next.js developer in Porto

Next.js for businesses in Porto, delivered remotely by one developer.

Next.js developer

My place in the WordPress community

  • I have had a wordpress.org account since 2006.
  • I contributed to WordPress core for versions 7.0 and 7.1.
  • I maintain 30 plugins on wordpress.org.
  • I organise and speak at WordCamps.
  • I have attended every WordCamp Europe from 2017 to 2026.
  • Local WordPress meetup: WordPress Porto Meetup

My WordPress.org profile

A Porto team that wants citizens to log in with the Cartao de Cidadao or the Chave Movel Digital does not build that login itself. It delegates it to Autenticacao.gov, the state authentication provider. I am Mariusz Szatkowski, a Next.js developer based in Gdynia. For this topic I build the part of the application that talks to Autenticacao.gov, and the interface around it, from your design or mine.

The Dom Luis I bridge over the Douro in Porto

Ponte Dom Luis I seen from the Douro waterfront in Porto. Taken by Diego Delso and shared under CC BY-SA 3.0 on Wikimedia Commons.

Everything below comes from the official Autenticacao.gov pages and the integration manual published there. I am not a lawyer, and whether your organisation may or must use the login is a question for you and the agency, not for a developer.

#What does Autenticacao.gov offer a web application?

According to its integration page, an organisation can offer registration and login with the Cartao de Cidadao or the Chave Movel Digital. The same page lists digital signature with the Chave Movel Digital, professional attributes certified through SCAP, and signing of electronic invoices through the SAFE system. Foreign citizens who hold a Chave Movel Digital can also reach the reserved area.

The manual describes the product as a single login for public administration sites. The citizen authenticates once and can use several services that start from different portals. Your application never sees the PIN or the card. It receives a signed answer and a set of attributes the citizen agreed to share.

#How does an organisation get access?

The integration page says both public and private organisations can join, and that the integration must be formalised in a protocol with ARTE, the Agencia para a Reforma Tecnologica do Estado. To join or ask questions, you email the agency; the address is on the integration page. The page gives no step-by-step procedure, so the first deliverable of any project here is that agreement, not code.

Once you join, the manual says the Autenticacao.gov team supplies the data needed for the integration. That includes an X.509 certificate with its chain for validating the responses, a certificate issued from the certificate signing request you send so the service can validate your requests, and the endpoints. The manual lists a test address and a production address for SAML requests, so the same code can run against both.

#Which flow suits a Next.js application?

The manual describes two web flows. The first is SAML 2.0 with the HTTP POST binding and a limited Web Browser SSO profile. The second is an OAuth2 implicit grant that returns an access token and then retrieves attributes in a separate REST step.

For a server-first Next.js application I would start with SAML, for one concrete reason. In the OAuth flow the manual shows, the token comes back in the fragment of a static return page (the example ends in Authorized#token_type=bearer&expires_in=86400&access_token=...). A URL fragment is read by the browser and is not sent to the server, so a Route Handler cannot see it. Client-side code would have to pick it up and pass it on. SAML posts the answer to an endpoint on your server, which is where signature validation belongs anyway.

Decide this with the agency. The manual I read is version 1.5.7 from November 2021, and I did not find a newer protocol description on the documents page.

#How does the SAML login work step by step?

The manual lists the steps. The citizen opens a private area, and your application generates a SAML AuthnRequest that identifies your organisation and lists the attributes you need. You redirect the citizen to the Autenticacao.gov login page. The service validates the request, asks the citizen to authorise the attributes, and asks them to prove who they are with the card or the Chave Movel Digital. It then builds a SAML Response and sends the citizen back to you.

Your application is responsible for validating that response and extracting the attributes. The manual says every SAML message is digitally signed, which protects integrity and identifies both parties.

The request is sent as a form POST with two fields, SAMLRequest (the Base64 encoded request) and RelayState. The manual says RelayState is for persisting state on your side, must not exceed 80 characters, and should carry its own integrity mechanism. It also says the service may modify the value and suggests Base64 encoding to avoid that.

#What attributes and trust levels can I request?

Attributes are requested in a SAML extension. The manual’s example asks for four, each with a name in the http://interop.gov.pt/MDC/Cidadao/ namespace: NIC, NIF, Foto and Morada. Each carries isRequired, so you can mark the card number as required and the rest as optional. The citizen can confirm or deny some or all of them.

Data minimisation is a design decision here, not a legal opinion from me. Request the attribute you need to match a user record, not the full set.

The same extension carries the minimum trust level, in an element called FAAALevel with a value from 1 to 4. The manual defines the levels as follows.

LevelAuthentication
4Cryptographic operation on the Cartao de Cidadao, or a client certificate from the notaries, lawyers or solicitors bodies
3Chave Movel Digital
2Chave Movel Digital through email or Twitter
1User and password, and social networks

The overall level of a login is the lowest level among the attributes obtained. If you leave FAAALevel out, the manual says the maximum level is assumed, so you receive only the attributes available at level 4. Treat the level as a product rule: a newsletter preferences page can accept a low level, a paid contract signature should ask for more.

#What does the Next.js code look like?

Two Route Handlers do the work. One builds the signed request and returns a page that auto-submits the form to the Autenticacao.gov address. The other receives the POST from the service, validates the signature, extracts the attributes and creates the session. A sketch of the receiving side, with the SAML validation left to a maintained SAML library you choose:

// app/auth/acs/route.ts (sketch)
import { NextResponse } from 'next/server'
import { validateSamlResponse } from '@/app/lib/saml' // your wrapper around a SAML library
import { createSession } from '@/app/lib/session'

export async function POST(request: Request) {
  const form = await request.formData()
  const samlResponse = String(form.get('SAMLResponse') ?? '')
  const relayState = String(form.get('RelayState') ?? '')

  const result = await validateSamlResponse(samlResponse, relayState)
  if (!result.ok) return new NextResponse(null, { status: 401 })

  await createSession(result.subjectId)
  return NextResponse.redirect(new URL('/area-reservada', request.url), 303)
}

One detail decides whether this works in a browser. The Next.js authentication guide sets the session cookie with SameSite: 'lax'. MDN states that a Lax cookie is sent on cross-site requests only for top-level navigations that use a safe method, which excludes POST. The citizen returns from Autenticacao.gov with a cross-site POST, so a Lax cookie set before the redirect will not arrive with it. Do not store the pending request in a Lax cookie and expect to read it back in the receiving handler. Keep the pending state on the server, keyed by the RelayState value, or use a short-lived cookie with SameSite=None and Secure, which MDN describes as sent with both cross-site and same-site requests. The RelayState limit of 80 characters fits a random opaque key and nothing larger.

The session you create after validation is a normal Next.js session. The guide recommends the Next.js cookies API with HttpOnly, Secure and an expiry, a Data Access Layer with a verifySession() function, and the Proxy file for optimistic checks only. It also says Server Actions and Route Handlers deserve the same checks as public endpoints.

#How do logout and single sign-on work?

Because Autenticacao.gov remembers that the citizen authenticated, the manual describes single sign-on between portals. Before showing a local login form, an entity portal can check whether the citizen is already authenticated and redirect them to the service automatically.

Logout is its own message. The manual says your portal redirects the citizen to Autenticacao.gov with a SAML LogoutRequest, signed with the X.509 certificate tied to your organisation, and consumes a signed logout response. Deleting your session cookie alone ends your session, not the single sign-on session. Build both: a Server Action that deletes the local session and a redirect that sends the logout request.

#What should the team check before building?

  • The agreement with ARTE and the contact on the integration page, before design work.
  • Which manual version applies today. Version 1.5.7 is dated November 2021.
  • Which signature algorithms the environment accepts. The signature example in the manual uses rsa-sha1, and I would ask the agency whether that is still the expectation.
  • Which attributes you really need, and the minimum trust level for each screen.
  • Where the pending request is stored between the redirect and the POST, given the cookie rule above.
  • A test citizen. The documents page lists a note about reading test Cartao de Cidadao cards, so ask the agency how test authentication is set up.

#What I deliver for a Porto team

I write the request and response Route Handlers, the session layer, the logout flow and the screens around them, in strict TypeScript. You get the repository, a runbook that lists every certificate and endpoint the code expects, and a test plan for the test environment. I do not hold your certificates unless you decide so, and the legal basis for using the login stays with your organisation.

If you have a Porto project that needs this login, send the scope through the contact page. The first written estimate is free, and the Next.js developer page explains the wider scope of work.

See also in Portugal

Need this service: Next.js developer in Porto?

Describe your project by e-mail. I reply within 24 hours.

Describe your project

Explore other WordPress services and knowledge base