Is there a way to seed users into accounts-password? Something like:
Users.insert
email: 'test#test.com'
password: 'secret'
I've tried many things but no luck yet. Thanks in advance for the help
It's easiest to do this on the server when it starts:
Meteor.startup(function() {
if (Meteor.users.find().count() === 0) {
Accounts.createUser({
username: 'test',
email: 'test#example.com',
password: 'password'
});
}
});
Related
How can i create a simple reset password using next auth Credential provider with mongo db
i have user schema like this
const userSchema = new mongoose.Schema<UserSchemaType>(
{
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
img: { type: String, required: true },
isAdmin: { type: Boolean, required: true, default: false },
},
{
timestamps: true,
}
);
my next auth look like this
providers: [
CredentialsProvider({
async authorize(credentials) {
await db.connect();
const user = await User.findOne({
email: credentials.email,
});
if (user && bcryptjs.compareSync(credentials.password, user.password)) {
return {
_id: user._id,
name: user.name,
email: user.email,
image: user.img,
isAdmin: user.isAdmin,
};
}
throw new Error('Invalid email or password');
},
}),
],
});
is there a simple example for implementing reset password with the next auth
From the docs - https://next-auth.js.org/providers/credentials
The functionality provided for credentials based authentication is
intentionally limited to discourage use of passwords due to the
inherent security risks associated with them and the additional
complexity associated with supporting usernames and passwords.
Probably you can use the Email Provider and customise if you really want this feature.
https://next-auth.js.org/configuration/providers/email
For the app I'm building, I want my users to have a profile created for them when they register; the profile would contain the user's username, email, and the uid created by firebase authentication. I've got the authentication portion using createUserWithEmailAndPassword to work on its own. I'm also able to create a "users" collection, capturing a username and the user's email, on its own as well. However, I'm running into trouble grabbing and saving the uid to the user's profile in the user's collection.
Here is the code I have at the moment:
import * as firebase from "firebase/app";
import db from "../../components/firebase/firebaseInit";
actions: {
registerUser({ commit }, payload) {
commit("setLoading", true);
commit("clearError");
firebase
.auth()
.createUserWithEmailAndPassword(payload.email, payload.password)
.then(user => {
commit("setLoading", false);
const newUser = {
email: user.email,
id: user.uid,
courses: []
};
commit("setUser", newUser);
db.collection("users")
.add({
username: payload.username,
email: user.email,
userId: user.uid
})
.then(() => {
console.log("New user added!");
})
.catch(err => {
console.log(err);
});
})
.catch(err => {
commit("setLoading", false);
commit("setError", err);
});
},
In the research I've done, I've found these suggested solutions:
Get Current User Login User Information in Profile Page - Firebase and Vuejs
Cloud Firestore saving additional user data
And this video:
https://www.youtube.com/watch?v=qWy9ylc3f9U
And I have tried using the set() method instead of add(), as well.
But none of them seem to work, for me at least.
Thank you in advance for your help.
And if you need to see any more code, just let me know.
You haven't shared the error message you get, but most probably the error comes from the fact that the createUserWithEmailAndPassword() method returns a UserCredential and not a User.
So you have to do as follows:
import * as firebase from "firebase/app";
import db from "../../components/firebase/firebaseInit";
actions: {
registerUser({ commit }, payload) {
commit("setLoading", true);
commit("clearError");
firebase
.auth()
.createUserWithEmailAndPassword(payload.email, payload.password)
.then(userCredential=> {
commit("setLoading", false);
const user = userCredential.user; // <-- Here is the main change
const newUser = {
email: user.email,
id: user.uid,
courses: []
};
commit("setUser", newUser);
return db.collection("users")
.add({
username: payload.username,
email: user.email,
userId: user.uid
});
})
.then(() => {
console.log("New user added!");
})
.catch(err => {
commit("setLoading", false);
commit("setError", err);
});
},
I am trying to access an endpoint with username and password but the console give me a 401()
this is my code:
created () {
this.$http.get(URL, {
username: 'xxxxxxx',
password: 'xxxxx'
}).then(response => {
console.log(response)
})
}
Is it the correct way to access an endpoint with VueJS?
You have to provide the username and password codified in Base64.
const encodedUserPswd = btoa(`${user}:${pswd}`);
axios.get(URL, {}, {
headers: { `Authorization: Basic ${encodedUserPswd}` },
}).then((response) => {
// ...
});
I'm using firebase auth system for login with facebook on my ionic app.
I'm trying to get profile data and save it into database after user login with database. You can check the how can i to do that with the following code.
User can login successfully but can not set data into database.
How can i solve that?
loginWithFacebook() {
this.facebook.login(['email']).then((response) => {
let credintial = firebase.auth.FacebookAuthProvider.credential(response.authResponse.accessToken);
firebase.auth().signInWithCredential(credintial).then(info => {
this.userEmail = info['email'];
this.userName = info['displayName'];
this.userUid = info['uid'];
});
});
this.afDatabase.database.ref(`dump/${this.userUid}`).set({
username: this.userName,
email: this.userEmail,
uid: this.userUid,
wallet: 0
}).then(data => {
this.navCtrl.setRoot(TabsPage);
});
}
Should be aware that you are using an asynchronous function and you should insert the record into database after successful sign in.
loginWithFacebook() {
this.facebook.login(['email']).then((response) => {
let credintial = firebase.auth.FacebookAuthProvider.credential(response.authResponse.accessToken);
firebase.auth().signInWithCredential(credintial).then(info => {
this.userEmail = info['email'];
this.userName = info['displayName'];
this.userUid = info['uid'];
this.afDatabase.database.ref(`dump/${this.userUid}`).set({
username: this.userName,
email: this.userEmail,
uid: this.userUid,
wallet: 0
}).then(data => {
this.navCtrl.setRoot(TabsPage);
});
});
});
}
I have a Meteor application with the following fixtures code:
/imports/startup/server/fixtures.js
import { Meteor } from 'meteor/meteor'
import { Accounts } from 'meteor/accounts-base'
if(Meteor.users.find().count === 0) {
const users = [
{username: "admin", email: "admin#testing.demo", profile: { name: "Admin" }, roles: ["admin"]},
{username: "school", email: "school#testing.demo", profile: { name: "School Name" }, roles: ["school"]},
{username: "teacher", email: "teacher#testing.demo", profile: { name: "Teacher" }, roles:["teacher"]}
]
for(let user of users) {
Accounts.createUser({
username: user.username,
email: user.email,
password: "123456",
profile: {
name: user.profile.name,
},
roles: user.roles
})
}
}
On starting up my project all the accounts are created successfully except none of them have the roles field. What am I doing wrong?
What you are doing wrong is that you pass options into the function that are not accepted by it. createUser options only accepts username, email, password and profile. You should study the docs, meteors API is very well documented.
Now, to set the user roles you have a couple of options, one of them would be use the _id of the newly created user, which is returned by createUser and then set the roles like so:
const userId = Accounts.createUser({
username: user.username,
email: user.email,
password: "123456",
profile: {
name: user.profile.name,
});
Roles.addUsersToRoles(userId, user.roles)
assuming that this is server side code. On the client this won't work. You could also set the roles directly using a Meteor.users.update(); call or fiddle around with Accounts.onCreateUser callback, which is very nice for manipulating everything that you pass into createUser. Hope that helps.