|
@@ -13,6 +13,8 @@ const expand = require('./expand')
|
|
|
const {Savable, slice, getModels} = await require('./models.js')()
|
|
|
const jwtGQL = require('./jwt')
|
|
|
|
|
|
+ const {anonSchema, anonResolvers} = require('./anon')({Savable, secret: jwtSecret})
|
|
|
+
|
|
|
let schema = buildSchema(`
|
|
|
type User {
|
|
|
_id: String
|
|
@@ -96,65 +98,6 @@ const expand = require('./expand')
|
|
|
schema = expand(schema)
|
|
|
console.log(printSchema(schema))
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- class User extends Savable {
|
|
|
-
|
|
|
- }
|
|
|
- Savable.addClass(User)
|
|
|
-
|
|
|
- const anonResolvers = {
|
|
|
- createUser:async function ({login, password}){
|
|
|
- let user = await Savable.m.User.findOne({login, password})
|
|
|
- if (user)
|
|
|
- return null;
|
|
|
- user = await (new User({login, password})).save()
|
|
|
-
|
|
|
- user.___owner = user._id.toString()
|
|
|
- user.___permissions = {
|
|
|
- read: ["owner", "user"]
|
|
|
- }
|
|
|
-
|
|
|
- return await user.save()
|
|
|
- },
|
|
|
-
|
|
|
- login: async function({login, password}){
|
|
|
- console.log(Savable.classes)
|
|
|
- const user = await Savable.m.User.findOne({login, password})
|
|
|
- if (!user)
|
|
|
- return null;
|
|
|
-
|
|
|
- const token = jwt.sign({ sub: {id: user._id, login}}, jwtSecret);
|
|
|
- return token
|
|
|
- },
|
|
|
-
|
|
|
- changePassword:async function ({login, password, newPassword}){
|
|
|
- const user = await Savable.m.User.findOne({login, password})
|
|
|
- if (!user) return null;
|
|
|
- user.password = newPassword;
|
|
|
- return await user.save()
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- const anonSchema = buildSchema(`
|
|
|
- type Query {
|
|
|
- login(login: String!, password: String!): String
|
|
|
- }
|
|
|
- type Mutation {
|
|
|
- createUser(login: String!, password: String!): User
|
|
|
- changePassword(login: String!, password: String!, newPassword: String!): User
|
|
|
- }
|
|
|
-
|
|
|
- type User {
|
|
|
- _id: String
|
|
|
- createdAt: String
|
|
|
- login: String
|
|
|
- nick : String
|
|
|
- }
|
|
|
-
|
|
|
- `)
|
|
|
-
|
|
|
const app = express();
|
|
|
app.use(express.static('public'));
|
|
|
app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
|