Expand description
Abstraction of AUTH command.
For general information about this command, see the Redis documentation.
Authentication is done automatically by ConnectionHandler, so there is usually no need for manual execution.
§Password-only
let mut stack = Stack::default();
let clock = StandardClock::default();
let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
let client = connection_handler.connect(&mut stack, Some(&clock)).unwrap();
// Cast from Credentials
let command = AuthCommand::from(&Credentials::password_only("secret123!"));
let _ = client.send(command);
// Directly creating Auth command:
let command = AuthCommand::new(None as Option<&str>, "secret123!");
let _ = client.send(command);
§Username/Password (ACL based authentication)
Requires Redis version > 6.0 + serverside ACL configuration
let mut stack = Stack::default();
let clock = StandardClock::default();
let mut connection_handler = ConnectionHandler::resp3(SocketAddr::from_str("127.0.0.1:6379").unwrap());
let client = connection_handler.connect(&mut stack, Some(&clock)).unwrap();
// Cast from Credentials
let command = AuthCommand::from(&Credentials::acl("user01", "secret123!"));
let _ = client.send(command);
// Directly creating Auth command:
let command = AuthCommand::new(Some("user01"), "secret123!");
let _ = client.send(command);
§Error handling
Successful execution is terminated by returning Ok(())
response.
Authentication errors are normally signalled by Redis with an error response, which is mapped to CommandErrors::ErrorResponse.
let command = AuthCommand::from(&Credentials::password_only("wrong_password"));
let result = client.send(command).unwrap().wait().unwrap_err();
assert_eq!(CommandErrors::ErrorResponse(error_string), result);