Im trying to make a web app in Flask with Pymongo and a MongoDB
Im getting that it cant find the attribute password in my db under users
elif request.method == 'POST':
login_user = request.form['username']
db_user = mongo.db.users.find({"username": "MattatMatt"})
pw = db_user.password
I know im being an idiot, please help.
This is the database:
username:"MattatMatt"
password:"..."
If you need anything else please ask.
Thank you!!!
find() returns a cursor. You likely want find_one() which returns a single record as a dict. As it's a dict and not an object, you will need to get the password using db_user['password'], e.g.
elif request.method == 'POST':
login_user = request.form['username']
db_user = mongo.db.users.find_one({"username": "MattatMatt"})
pw = db_user['password']
Related
I have a MongoDB database where I overwrote the _id field manually using some generated tags (duplicates are not an issue).
Trouble is, I am trying to find_one searching by _id but it continuously returns None and I cannot figure it out for the life of me. Any help would be appreciated.
Here is the code I have so far:
async def update(self):
#self.input is part of my class variables and comes in as a string
client = AsyncIOMotorClient(sensitive_data.mongoDB_server_address) #sensitive data is another python file with the server address/usernae/password
db = client.Database
accounts = db.accounts
print(type(self.input)) #this prints string
account = await accounts.find_one({'_id':self.input})
print(account) #this prints none
return account
Edit:
I found some information on pyMongo documentation about ObjectID. I tried implementing it with this code:
from bson.objectid import ObjectId
async def update(self):
#self.input is part of my class variables and comes in as a string
client = AsyncIOMotorClient(sensitive_data.mongoDB_server_address) #sensitive data is another python file with the server address/usernae/password
db = client.Database
accounts = db.accounts
print(type(self.input)) #this prints string
account = await accounts.find_one({'_id':ObjectID(self.input)})
print(account) #this prints none
return account
But get this traceback:
bson.errors.InvalidId: 'mystring' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string
I figured it out. I was trying to find an ID in a database that didn't exist. Oops.
I did put password of that user in the field and i have tried all possible combination with dbname.I dont know which dbname it is referring to. I have searched many places, didnt get any answers. Can some one please help me how to configure.
app.config['DEBUG']=True
app.config['MONGOALCHEMY_CONNECTION_STRING']='mongodb+srv://user:
<password>#test.usvae.mongodb.net/<dbname>?retryWrites=true&w=majority'
db=MongoAlchemy(app)
This is my configeration.
This is the error i am getting
raise ImproperlyConfiguredError("You should provide a database name "
flask_mongoalchemy.ImproperlyConfiguredError: You should provide a database name (the MONGOALCHEMY_DATABASE setting)
Thanks in advance
import pymongo
## DB Connection ##
client = pymongo.MongoClient("mongodb+srv://mongouser:password#<cluster>/<db>?retryWrites=true&w=majority")
## DB Creation ##
db = client.<db>
## Collection Creation ##
col1 = db.Users
if client:
print("connected")
else:
print("not connected")
# Single Value Insert ##
Users = {"ID":"481292","Name":"DS"}
#x1 = col1.insert_one(Users)
You can try above code to connect mongodb atlas to flask.
Below code for using mongodb atlas with flask_mongoalchemy
from flask import Flask
from flask_mongoalchemy import MongoAlchemy
app = Flask(__name__)
DB_URI = 'mongodb+srv://<user>:<password>#<cluster>/<db>?retryWrites=true&w=majority'
app.config['MONGOALCHEMY_DATABASE'] = 'test'
app.config["MONGODB_HOST"] = DB_URI
db = MongoAlchemy(app)
class Users(db.document):
name = db.StringField()
age = db.IntField()
You need to insert value as well, to avoid error.
I have a grails application in which Im using db.authenticate for a login page but I understand this method has been deprecated and therefore I would like to upgrade my application to using the MongoCredential object for authentication. However, unlike the db.authenticate which nicely returns a boolean to get authentication done, the MongoCredential doesn't return a boolean so how can I go about accomplishing the code replacement with minimal headache. Ideally, I'd like to derive some kind of a boolean to tell me if authentication was achieved. Thanks for your patience. Im a newbie with Mongodb.
This is part of the code I need to replace which currently makes use of the deprecated method "authenticate":
MongoClient mongoClient = new MongoClient("localhost", 27017)
DB db = mongoClient.getDB("twcdb");
def userName = params.username
def passWord = params.password
//deprecated method being used in the line below.
boolean auth = db.authenticate(userName, passWord.toCharArray())
if (auth) {
userloggedin = params.username
render(contentType: 'text/json') {
[success: true, url: createLink(controller: 'admin', action: 'loggedin')]
}
}
else {
render(contentType: 'text/json') {
["success": false, "message": 'Login or Password is incorrect.']
}
Edit: I know that the answer must lie in testing a property of the MongoClient object somehow to see if it contains a valid authenticated connection but I am still stuck on how to do this. Given I knowingly feed the MongoClient constructor with a bogus MongoCredential, it still creates an object that isn't null. I was betting on the null test but no joy. So, how do I replace the deprecated db.authenticate?
I am trying to integrate Facebook authentication into a Rails application with mongomapper and I am getting an error on the following method. I think the error is in the first_or_initialize section because it is ActiveRecord query instead of mongomapper.
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
EDIT:
Any suggestions?
I'm in the same boat. I was able to create the user by changing the first_or_initialize line to this:
first_or_create(auth.slice(:provider, :uid)).tap do |user|
I'm working on a flask app that needs authentication. I've hooked up flask-login but it doesn't seem very graceful.
First flask-login needs to make sure the user exists:
#login_manager.user_loader
def load_user(id):
return User.query.get(id)
But you also need to use 'login_user' to create the user object
# Some code above
user = User.query.filter_by(email = form.email.data, password = form.password.data).first()
user.login_status = 1
db.session.commit()
login_user(objects.SignedInUser(user.id, user.email, user.login_status == LoginStatus.Active))
# Some code below
In the code above 'User' is a model for postgres and SignedInUser is just an object to be used for flask-login.
Does anyone have an example of flask-login used with postgres?
It looks like you might be misunderstanding what Flask-Login handles. It's there to keep track of everything about the user's session after you tell it authentication was successful (by calling login_user.) The user_loader callback only tells it how to reload the object for a user that has already been authenticated, such as when someone reconnects to a "remember me" session. The docs are not especially clear on that.
There should be no need to keep a flag in the database for the user's login status. Also, the code you included will raise an AttributeError if the credentials are incorrect (user = None).
Here's an example from a Flask-SQLAlchemy application. It uses an external authentication source and a wrapper for the SQLAlchemy User object, but the process is basically the same.
user_loader callback:
#login_manager.user_loader
def load_user(user_id):
user = User.query.get(user_id)
if user:
return DbUser(user)
else:
return None
User class (wrapper for SQLAlchemy object):
# User class
class DbUser(object):
"""Wraps User object for Flask-Login"""
def __init__(self, user):
self._user = user
def get_id(self):
return unicode(self._user.id)
def is_active(self):
return self._user.enabled
def is_anonymous(self):
return False
def is_authenticated(self):
return True
Login handler:
#app.route('/login', methods=['GET', 'POST'])
def login():
error = None
next = request.args.get('next')
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if authenticate(app.config['AUTH_SERVER'], username, password):
user = User.query.filter_by(username=username).first()
if user:
if login_user(DbUser(user)):
# do stuff
flash("You have logged in")
return redirect(next or url_for('index', error=error))
error = "Login failed"
return render_template('login.html', login=True, next=next, error=error)
Note that login fails if:
external auth fails
user query returns None (user does not exist)
login_user returns False (user.is_active() == False)
Logout
#app.route('/logout')
#login_required
def logout():
logout_user()
flash('You have logged out')
return(redirect(url_for('login')))