Authenticate with the server. `database` — optional target database name. When set it is sent in the auth frame so the server can bind the connection's database context at handshake time (equivalent to `psql -d `).
(
&mut self,
method: AuthMethod,
database: Option<&str>,
)
| 184 | /// the auth frame so the server can bind the connection's database |
| 185 | /// context at handshake time (equivalent to `psql -d <name>`). |
| 186 | pub async fn authenticate( |
| 187 | &mut self, |
| 188 | method: AuthMethod, |
| 189 | database: Option<&str>, |
| 190 | ) -> NodeDbResult<()> { |
| 191 | let resp = self |
| 192 | .send( |
| 193 | OpCode::Auth, |
| 194 | TextFields { |
| 195 | auth: Some(method), |
| 196 | database: database.map(|s| s.to_string()), |
| 197 | ..Default::default() |
| 198 | }, |
| 199 | ) |
| 200 | .await?; |
| 201 | |
| 202 | if resp.status == ResponseStatus::Error { |
| 203 | let msg = resp |
| 204 | .error |
| 205 | .map(|e| e.message) |
| 206 | .unwrap_or_else(|| "auth failed".into()); |
| 207 | return Err(NodeDbError::authorization_denied(msg)); |
| 208 | } |
| 209 | |
| 210 | self.authenticated = true; |
| 211 | Ok(()) |
| 212 | } |
| 213 | |
| 214 | /// Send a ping and await the pong. |
| 215 | pub async fn ping(&mut self) -> NodeDbResult<()> { |