SurrealDB Docs Logo

Enter a search query

invalidate()

Invalidates the authentication for the current connection.

Method Syntax
db.invalidate()

Example usage

Note: the following example uses the ureq crate with the json feature to first send a request to the database’s /signup endpoint which returns a token. The reqwest crate and others can be used here instead.

Alternatively, you could use a command like curl -X POST -H "Accept: application/json" -d '{"ns":"test","db":"test","ac":"account","user":"info@surrealdb.com","pass":"123456"}' http://localhost:8000/signup, copy the returned token, and paste it into the .authenticate() method which is used before .invalidate(). As the DEFINE ACCESS method below shows, a token will remain valid by default for 15 minutes.

// Use the following statement to set up the access // // DEFINE ACCESS account ON DATABASE TYPE RECORD // SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) ) // SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) ) // DURATION FOR TOKEN 15m, FOR SESSION 12h // ; // DEFINE TABLE cat SCHEMALESS // PERMISSIONS for select, update, delete, create // WHERE $auth.id; use serde::Deserialize; use std::fmt::Display; use surrealdb::engine::remote::ws::Ws; use surrealdb::Surreal; #[derive(Deserialize)] struct Response { token: String, } impl Display for Response { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.token) } } #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = Surreal::new::<Ws>("127.0.0.1:8000").await?; let token = ureq::post("http://127.0.0.1:8000/signup") .set("Accept", "application/json") .send_json(ureq::json!({ "ns": "ns", "db": "db", "ac": "account", "email": "info@surrealdb.com", "pass": "123456" })) .unwrap() .into_json::<Response>() .unwrap() .to_string(); db.authenticate(token).await?; // User is present inside the $auth parameter dbg!(db.query("RETURN $auth").await?); db.invalidate().await?; // User is now gone dbg!(db.query("RETURN $auth;").await?); Ok(()) }

See also

On this page

© SurrealDB GitHub Discord Community Cloud Features Releases Install