appwrite: π Bug Report: User login
π Reproduction steps
After sign up, I tried to login but it says Error logging in: Invalid `userId` param: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can't start with a special char
π Expected behavior
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Log In</title> <link rel="stylesheet" href="../styles/login.css" /> <script src="../dist/login.bundle.js" defer></script> </head> <body>Log In
<form id="login-form"> <label for="email">Email:</label><label for="password">Password:</label>
<button type="submit">Log In</button> </form> </body> </html>
login.js import { Client, Account } from βappwriteβ;
const client = new Client(); client .setEndpoint(βhttps://cloud.appwrite.io/v1β) .setProject(β6670bced001681b60586β);
const account = new Account(client);
document .getElementById(βlogin-formβ) .addEventListener(βsubmitβ, function (event) { event.preventDefault();
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
account
.createSession(email, password)
.then((response) => {
console.log("User logged in:", response);
alert("Login successful!");
document.getElementById("login-form").reset();
window.location.href = "/pages/news-today.html";
})
.catch((error) => {
console.error("Error logging in:", error);
alert("Error logging in: " + error.message);
});
});
FROM BROWSER NETWORK TAB:
Request URL:
https://cloud.appwrite.io/v1/account/sessions/token
Request Method:
POST
Status Code:
400 Bad Request
signup.html -- this was successful and user can be added to the dashboad
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Sign Up</title>
<link rel="stylesheet" href="/styles/signup.css" />
<!-- Adjust path if necessary -->
<script src="/dist/signup.bundle.js" defer></script>
<!-- Correct path to the bundle -->
</head>
<body>
<h1>Sign Up</h1>
<form id="signup-form">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required /><br />
<label for="email">Email:</label>
<input type="email" id="email" name="email" required /><br />
<label for="password">Password:</label>
<input type="password" id="password" name="password" required /><br />
<button type="submit">Sign Up</button>
</form>
</body>
</html>
signup.js
import { Client, Account, ID } from "appwrite";
// Initialize the Appwrite client
const client = new Client();
client
.setEndpoint("https://cloud.appwrite.io/v1") // Set the endpoint of your Appwrite server
.setProject("6670bced001681b60586"); // Set your project ID
// Create an Account instance
const account = new Account(client);
// Add an event listener to the signup form submission
document
.getElementById("signup-form")
.addEventListener("submit", function (event) {
event.preventDefault();
// Get the user input values
const username = document.getElementById("username").value;
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
// Create a new user account
account
.create(ID.unique(), email, password, username)
.then((response) => {
console.log("User created:", response);
alert("User created successfully!");
// Inform the user to check their email for verification
alert("Please check your email for a verification link.");
document.getElementById("signup-form").reset();
setTimeout(() => {
window.location.href = "/pages/login.html";
}, 2000); // 2-second delay
})
.catch((error) => {
console.error("Error creating user:", error);
alert("Error creating user: " + error.message);
});
});
π Actual Behavior
I need to log in
π² Appwrite version
Appwrite Cloud
π» Operating system
Linux
π§± Your Environment
Web
π Have you spent some time to check if this issue has been raised before?
- I checked and didnβt find similar issue
π’ Have you read the Code of Conduct?
- I have read the Code of Conduct
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (2 by maintainers)
Hey @Jedidiah-Solomon, the correct login method for email and password is
account.createEmailPasswordSession('email@example.com', 'password')Youβre getting an error message because
account.createSession()expects aUSER_IDandSECRETargument. Youβd typically use it when working with Magic URL, phone auth, email OTP, and custom token auth.