adrianhajdin / project_medical_pager_chat

Medical Pager Chat App powered by GetStream
1.5k stars 466 forks source link

Shows Internal error , api client-server token #19

Open abins6342 opened 2 years ago

abins6342 commented 2 years ago

const stream = require('getstream'); const bcrypt = require('bcrypt'); const StreamChat = require('stream-chat').StreamChat; const crypto = require('crypto');

require('dotenv').config();

const api_key = process.env.STREAM_API_KEY; const api_secret = process.env.STREAM_API_SECRET; const app_id = process.env.STREAM_APP_ID;

const signup = async (req, res) => { try {

    const { fullName, username, password, phoneNumber } = req.body;

    const userId = crypto.randomBytes(16).toString('hex');

    const serverClient = stream.connect(api_key, api_secret, app_id);

    const hashedPassword = await bcrypt.hash(password, 10);

    const token = serverClient.createUserToken(userId);

    res.status(200).json({ token, fullName, username, userId, hashedPassword, phoneNumber });
} catch (error) {
    console.log(error);

    res.status(500).json({ message: error });
}

};

const login = async (req, res) => { try {

    const { username, password } = req.body;

    const serverClient = stream.connect(api_key, api_secret, app_id);
    const client = StreamChat.getInstance(api_key, api_secret);
    if (!client.api_secret){
        client.api_secret=process.env.STREAM_API_SECRET
    }

    const { users } = await client.queryUsers({ name: username });

    if(!users.length) return res.status(400).json({ message: 'User not found' });

    const success = await bcrypt.compare(password, users[0].hashedPassword);

    const token = serverClient.createUserToken(users[0].id);

    if(success) {
        res.status(200).json({ token, fullName: users[0].fullName, username, userId: users[0].id});
    } else {
        res.status(500).json({ message: 'Incorrect password' });
    }
} catch (error) {
    console.log(error);

    res.status(500).json({ message: error });
}

};

module.exports = { signup, login }

pwn-mm commented 1 year ago

For me, it was because of axios version. Check package.json.