Redirect based on user login

Hi everybody,

Is there an easy way to redirect a user based on his authentication login and password ?


Hi @zelda-rageux,

This is extremely dependent on what technology you use for authentication. Personally I’d recommend doing this kind of operation on your backend, rather than your webserver.

Assuming you’re talking about basic auth, it is possible that the redir directive could make conditional comparisons against the Authorization request header sent by the client.

You would need to work out beforehand what the header would contain and then test for it directly. The Authorization header usually consists of the text Basic [STRING] where [STRING] is the base-64 encoded version of [USER]:[PASSWORD].

The test would look like this:

basicauth / user password
redir {
  # Valid only for literal user:password
  if {>Authorization} is "Basic dXNlcjpwYXNzd29yZA=="

basicauth / foo bar
redir {
  # Valid only for literal foo:bar
  if {>Authorization} is "Basic Zm9vOmJhcg=="

1 Like

Hi Matthew,

This solution is fine for me, i’ll produce a new Docker image with new Caddyfile automatically when the users db change.

Thanks you very much for your help.

1 Like

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