YCPCS-481-TeamB / BaseballSim

CS481 Group Project
0 stars 0 forks source link

Add Team Issue #32

Closed koopaluigi closed 7 years ago

koopaluigi commented 7 years ago

I am trying to add a team to my user in the iOS app but I am not sure how to get the req.userdata in the post method:

// teams.js

router.post('/', function(req, res, next){
    var teamname = req.body.teamname || "Untitled";
    var league_id = req.body.league_id || 0;
    TeamsController.buildRandomTeam(teamname, league_id).then(function(response){
        PermissionController.addPermission('teams', response.id, req.userdata.id).then(function(data){
            res.status(200).json({success: true, teams: response});
        }).catch(function(err){
            res.status(200).json({success: false, error:""+ err});
        })
    }).catch(function(err){
        res.status(200).json({success: false, error:""+ err});
    });
});

In the get methods I use the token to get information but I'm not sure how to get the userdata. These are my methods in the iOS app:

//TeamService.swift

func addPlayer(name:String, league_id:String)
    {
        let request = DispatchGroup.init()

        //Gets token of user if user exists

        //let url = apiUrl.api + "/teams/"
        let url = "https://baseballsim-koopaluigi.c9users.io/api/teams/"
        print("URL: \(url)")
        params = ["teamname": name, "league_id" : league_id]

        request.enter()

        requests.postRequest(url: url, params: (params as [String : AnyObject]?)!, headers: headers, finished: {
            () in
            request.leave()
        })

        request.wait()

    }
//Request.swift

func postRequest(url:String, params: [String:AnyObject]?, headers: [String:String]?, finished: @escaping () -> Void)
    {
        guard let thisUrl = URL(string: url) else
        {
            print("Error: URL is invalid")
            return
        }

        var request = URLRequest(url: thisUrl)

        request.httpMethod = "POST"

        //Set parameters
        var body = ""
        //Combines parameters into string to be placed into the body
        for (i, param) in (params?.enumerated())!
        {
            body += "\(param.key)=\(param.value)"
            if i != (params?.count)!-1
            {
                body += "&"
            }
        }

        request.httpBody = body.data(using: String.Encoding.utf8)

        for header in headers!
        {
            request.addValue(header.key, forHTTPHeaderField: header.value)
        }

        let task = URLSession.shared.dataTask(with: request)
        {
            data, response, error -> Void in

            //Check for an error
            if error != nil
            {
                print("Error in POST: \(error)")
                return
            }

            /*
             //Print out the response from the server for debugging
             let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
             print("responseString = \(responseString)")
             */

            var post:NSDictionary = [:]

            //Convert json into NSDictionary
            do
            {
                post = try JSONSerialization.jsonObject(with: data!, options: []) as! NSDictionary
            }
            catch let error as NSError
            {
                print("ERROR: \(error.localizedDescription)")
            }
            self.postDictionary = post

            //Task is finished
            finished()
        }

        task.resume()

    }
bfwalton commented 7 years ago

The userdata is added in through the middleware the authentication process. As long as you are passing the token in the "x-access-token" header it will be passed automatically

bfwalton commented 7 years ago

If you want to get the user data from the token you can POST to "api/users/validate" and that will return the associated users data. You can also use this endpoint to determine if the user's token is valid