Skip to content
This repository has been archived by the owner on Oct 18, 2021. It is now read-only.

Add auth to base examples #256

Closed
marcelbuesing opened this issue Jun 21, 2018 · 4 comments
Closed

Add auth to base examples #256

marcelbuesing opened this issue Jun 21, 2018 · 4 comments

Comments

@marcelbuesing
Copy link

This might be common knowledge but I was not aware that there was the need for calling auth with user and password after already providing it via uri already.

So:

let client = Client::with_uri("mongodb://x:y@localhost:27017")?;

turned out to be

let client = Client::with_uri("mongodb://x:y@localhost:27017")?;
client.auth("x", "y");

Is this a bug or am I missunderstanding user and pass in the uri ?

@kyeah
Copy link
Contributor

kyeah commented Jun 23, 2018

Thanks for the issue! user-password authentication occurs at the database-level. The user, password, and database are parsed from the URI, but I don't believe we have it set up to automatically authenticate when you create the database object, which is probably desirable from a user standpoint.

@HugoSanchez
Copy link

Hi,

I get:

No method named 'auth' for found type 'std::sync::Arc::<mongodb::ClientInner>' in the current scope.

I'm also not able to use '?' in the previous line were I connect 'with_uri'.

What am I missing here?

@jcdyer
Copy link

jcdyer commented Dec 4, 2019

Sorry this is a year late, but for posterity, auth is on the db object. So try:

let db = client.db("appdb")?;
db.auth("x", "y")?;

You can only use ? in functions that return Result<T, E>. If you are in a function that returns something else, you'll need to handle the error yourself. The most flexible way is:

let db = match client.db("appdb") {
    Ok(db) => db,
    Err(err) => /* Do something else */
};
if let Err(err) => db.auth("username", "password") {
    eprintln!("Could not authenticate: {:?}", err)
}

If all you want to do is exit the program when something fails, you can do:

let db = client.database("appdb").expect("could not connect to database appdb");
db.auth("username", "password").expect("could not authenticate to database appdb");

@saghm
Copy link
Contributor

saghm commented Dec 19, 2019

Hello everyone! We've just released an alpha of an official MongoDB Rust driver; you can read about it here! Because of this, we won't be updating this driver anymore, and the repository will be made read-only as soon as I get finished posting these messages. Thanks for using this driver, and we hope you try out the new one!

@saghm saghm closed this as completed Dec 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants